琐碎的知识点

it2022-05-05  167

1、Python assert的使用 断言,使用assert关键字后面接一个条件表达式

如果条件表达式为真,意味着程序的当前条件与开发人员的自己断言的情况一样,则程序继续运行如果为假,则表明一定在前面发生了错误,则程序停止运行,抛出异常 例: assert 1!=1 #断言1不能于1

2、tensorflow由于版本问题出现的错误 问题:TypeError: Value passed to parameter ‘shape’ has DataType float32 not in list of allowed values: int32, int64 解决方案:根据控制台的提示,找到shape这个方法进行数据类型转换。np.int(…) 问题:ValueError: Only call softmax_cross_entropy_with_logits with named arguments (labels=…, logits=…, …)

解决方法:按照提示,需要将括号内的形参写出,即logits=outputs, labels=targets而非(outputs,targets) 3、np.newaxis

x1 = np.array([1, 2, 3, 4, 5]) print(x1) # the shape of x1 is (5,) x1_new = x1[:, np.newaxis] print(x1_new) # now, the shape of x1_new is (5, 1) # [[1], # [2], # [3], # [4], # [5]] x1_new = x1[np.newaxis,:] print(x1_new) # now, the shape of x1_new is (1, 5) # [[1, 2, 3, 4, 5]]

3、tf.argmax() tf.argmax(input,axis)返回每行或者每列的最大值的索引(位置),其中,axis=0表示比较每一列的元素,返回每一列的元素的最大值索引:

import tensorflow as tf test = np.array([[1, 2, 5], [2, 5, 4], [5, 4, 3], [8, 7, 2]]) print(test) print("-----") with tf.Session(): print(tf.argmax(test,1).eval())


最新回复(0)