赞
踩
下面这段代码:
w1 = tf.Variable(tf.random_normal([2,3], stddev=1, seed=1))
w2 = tf.Variable(tf.random_normal([3,1], stddev=1, seed=1))
x = tf.constant([[0.7, 0.9]])
开始写成x = tf.constant([0.7, 0.9]),运行时总是出错:
Traceback (most recent call last):
File "<ipython-input-9-60e968c8a781>", line 1, in <module>
runfile('/home/yong/qx.py', wdir='/home/yong')
File "/home/yong/anaconda2/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 710, in runfile
execfile(filename, namespace)
File "/home/yong/anaconda2/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 93, in execfile
builtins.execfile(filename, *where)
File "/home/yong/qx.py", line 13, in <module>
a = tf.matmul(x, w1)
File "/home/yong/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 1844, in matmul
a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
File "/home/yong/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 1289, in _mat_mul
transpose_b=transpose_b, name=name)
File "/home/yong/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/home/yong/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2632, in create_op
set_shapes_for_outputs(ret)
File "/home/yong/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1911, in set_shapes_for_outputs
shapes = shape_func(op)
File "/home/yong/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1861, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/home/yong/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 595, in call_cpp_shape_fn
require_shape_fn)
File "/home/yong/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 659, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Shape must be rank 2 but is rank 1 for 'MatMul_21' (op: 'MatMul') with input shapes: [2], [2,3].
今天知道了原因,少写了一对括号。
也许,一对括号表示的是列表,两对括号才表示矩阵。矩阵要与矩阵相乘,导致了前面的错误。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。