我目前正在翻译一些Python代码,具体来说是神经网络和深度学习。
为了确保数据结构正确翻译,需要了解Python中嵌套类型的详细信息。虽然type()函数可以处理简单类型,但无法处理嵌套类型。
例如,在Python中:
返回独立于值的签名。
简而言之,我目前正在使用带有调试器的PyCharm,在变量窗口中点击查看单个变量的选项以查看详细信息。问题在于输出包含值和类型混合在一起,而我只需要类型签名。当变量像(float[50000][784], int[50000])这样时,值会妨碍阅读。是的,我现在正在重新调整变量大小,但这只是一个解决方法,不是解决方案。
例如:使用 PyCharm社区
使用 Spyder 使用Visual Studio Community与Python Tools for Visual Studio。
我不认为它已经完成了,但迄今为止它在我需要的所有东西上都起作用了。
为了确保数据结构正确翻译,需要了解Python中嵌套类型的详细信息。虽然type()函数可以处理简单类型,但无法处理嵌套类型。
例如,在Python中:
> data = ([[1,2,3],[4,5,6],[7,8,9]],["a","b","c"])
> type(data)
<type 'tuple'>
只给出第一级的类型,关于元组中的数组没有任何信息。
我希望得到像 F# 一样的东西。
> let data = ([|[|1;2;3|];[|4;5;6|];[|7;8;9|]|],[|"a";"b";"c"|]);;
val data : int [] [] * string [] =
([|[|1; 2; 3|]; [|4; 5; 6|]; [|7; 8; 9|]|], [|"a"; "b"; "c"|])
返回独立于值的签名。
这个可以用Python实现吗?int [] [] * string []
* is a tuple item separator int [] [] is a two dimensional jagged array of int string [] is a one dimensional array of string
简而言之,我目前正在使用带有调试器的PyCharm,在变量窗口中点击查看单个变量的选项以查看详细信息。问题在于输出包含值和类型混合在一起,而我只需要类型签名。当变量像(float[50000][784], int[50000])这样时,值会妨碍阅读。是的,我现在正在重新调整变量大小,但这只是一个解决方法,不是解决方案。
例如:使用 PyCharm社区
(array([[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
...,
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.]], dtype=float32),
array([7, 2, 1, ..., 4, 5, 6]))
使用 Spyder 使用Visual Studio Community与Python Tools for Visual Studio。
(array([[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
...,
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.]], dtype=float32),
array([5, 0, 4, ..., 8, 4, 8], dtype=int64))
编辑:
由于有人正在寻找更多细节,因此这是我的修改版本,它还可以处理numpy ndarray。感谢Vlad提供的初始版本。
另外,由于使用了Run Length Encoding的变体,不再使用?用于异构类型。
# Note: Typing for elements of iterable types such as Set, List, or Dict
# use a variation of Run Length Encoding.
def type_spec_iterable(iterable, name):
def iterable_info(iterable):
# With an iterable for it to be comparable
# the identity must contain the name and length
# and for the elements the type, order and count.
length = 0
types_list = []
pervious_identity_type = None
pervious_identity_type_count = 0
first_item_done = False
for e in iterable:
item_type = type_spec(e)
if (item_type != pervious_identity_type):
if not first_item_done:
first_item_done = True
else:
types_list.append((pervious_identity_type, pervious_identity_type_count))
pervious_identity_type = item_type
pervious_identity_type_count = 1
else:
pervious_identity_type_count += 1
length += 1
types_list.append((pervious_identity_type, pervious_identity_type_count))
return (length, types_list)
(length, identity_list) = iterable_info(iterable)
element_types = ""
for (identity_item_type, identity_item_count) in identity_list:
if element_types == "":
pass
else:
element_types += ","
element_types += identity_item_type
if (identity_item_count != length) and (identity_item_count != 1):
element_types += "[" + `identity_item_count` + "]"
result = name + "[" + `length` + "]<" + element_types + ">"
return result
def type_spec_dict(dict, name):
def dict_info(dict):
# With a dict for it to be comparable
# the identity must contain the name and length
# and for the key and value combinations the type, order and count.
length = 0
types_list = []
pervious_identity_type = None
pervious_identity_type_count = 0
first_item_done = False
for (k, v) in dict.iteritems():
key_type = type_spec(k)
value_type = type_spec(v)
item_type = (key_type, value_type)
if (item_type != pervious_identity_type):
if not first_item_done:
first_item_done = True
else:
types_list.append((pervious_identity_type, pervious_identity_type_count))
pervious_identity_type = item_type
pervious_identity_type_count = 1
else:
pervious_identity_type_count += 1
length += 1
types_list.append((pervious_identity_type, pervious_identity_type_count))
return (length, types_list)
(length, identity_list) = dict_info(dict)
element_types = ""
for ((identity_key_type,identity_value_type), identity_item_count) in identity_list:
if element_types == "":
pass
else:
element_types += ","
identity_item_type = "(" + identity_key_type + "," + identity_value_type + ")"
element_types += identity_item_type
if (identity_item_count != length) and (identity_item_count != 1):
element_types += "[" + `identity_item_count` + "]"
result = name + "[" + `length` + "]<" + element_types + ">"
return result
def type_spec_tuple(tuple, name):
return name + "<" + ", ".join(type_spec(e) for e in tuple) + ">"
def type_spec(obj):
object_type = type(obj)
name = object_type.__name__
if (object_type is int) or (object_type is long) or (object_type is str) or (object_type is bool) or (object_type is float):
result = name
elif object_type is type(None):
result = "(none)"
elif (object_type is list) or (object_type is set):
result = type_spec_iterable(obj, name)
elif (object_type is dict):
result = type_spec_dict(obj, name)
elif (object_type is tuple):
result = type_spec_tuple(obj, name)
else:
if name == 'ndarray':
ndarray = obj
ndarray_shape = "[" + `ndarray.shape`.replace("L","").replace(" ","").replace("(","").replace(")","") + "]"
ndarray_data_type = `ndarray.dtype`.split("'")[1]
result = name + ndarray_shape + "<" + ndarray_data_type + ">"
else:
result = "Unknown type: " , name
return result
我不认为它已经完成了,但迄今为止它在我需要的所有东西上都起作用了。
[1, 2,'c']
应该是什么类型? - L3viathandata
来自可预测的、有结构的来源吗?还是仅仅是偶然的呢? - Kyle Pittman