我是tensorflow的新手,并且我使用tf.matmul
运行了这段代码,
起初-我不明白为什么matmul
中的形状不好。-我用另一个[] int定义了变量,将其固定。
现在-我不明白为什么它仍然无法正常工作。
import tensorflow as tf
W = tf.Variable([[.3]], tf.float32)
b = tf.Variable([[-.3]], tf.float32)
x = tf.placeholder(tf.float32)
linear_model = tf.matmul(x, W) + b
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(linear_model, {x: [[1, 2, 3, 4]]}))
C:\Users\hagayj\AppData\Local\Programs\Python\Python35\python.exe "C:/Users/hagayj/OneDrive/לימודים/untitled1/Defining weights as variables in a linear model.py"
2018-11-05 20:21:31.580447: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
File "C:\Users\hagayj\AppData\Roaming\Python\Python35\site-packages\tensorflow\python\client\session.py", line 1292, in _do_call
return fn(*args)
File "C:\Users\hagayj\AppData\Roaming\Python\Python35\site-packages\tensorflow\python\client\session.py", line 1277, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "C:\Users\hagayj\AppData\Roaming\Python\Python35\site-packages\tensorflow\python\client\session.py", line 1367, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Matrix size-incompatible: In[0]: [1,4], In[1]: [1,1]
[[{{node MatMul}} = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_Placeholder_0_0, Variable/read)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<encoding error>", line 14, in <module>
File "C:\Users\hagayj\AppData\Roaming\Python\Python35\site-packages\tensorflow\python\client\session.py", line 887, in run
run_metadata_ptr)
File "C:\Users\hagayj\AppData\Roaming\Python\Python35\site-packages\tensorflow\python\client\session.py", line 1110, in _run
feed_dict_tensor, options, run_metadata)
File "C:\Users\hagayj\AppData\Roaming\Python\Python35\site-packages\tensorflow\python\client\session.py", line 1286, in _do_run
run_metadata)
File "C:\Users\hagayj\AppData\Roaming\Python\Python35\site-packages\tensorflow\python\client\session.py", line 1308, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Matrix size-incompatible: In[0]: [1,4], In[1]: [1,1]
[[{{node MatMul}} = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_Placeholder_0_0, Variable/read)]]
Caused by op 'MatMul', defined at:
File "<encoding error>", line 7, in <module>
File "C:\Users\hagayj\AppData\Roaming\Python\Python35\site-packages\tensorflow\python\ops\math_ops.py", line 2053, in matmul
a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
File "C:\Users\hagayj\AppData\Roaming\Python\Python35\site-packages\tensorflow\python\ops\gen_math_ops.py", line 4856, in mat_mul
name=name)
File "C:\Users\hagayj\AppData\Roaming\Python\Python35\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\Users\hagayj\AppData\Roaming\Python\Python35\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "C:\Users\hagayj\AppData\Roaming\Python\Python35\site-packages\tensorflow\python\framework\ops.py", line 3272, in create_op
op_def=op_def)
File "C:\Users\hagayj\AppData\Roaming\Python\Python35\site-packages\tensorflow\python\framework\ops.py", line 1768, in __init__
self._traceback = tf_stack.extract_stack()
InvalidArgumentError (see above for traceback): Matrix size-incompatible: In[0]: [1,4], In[1]: [1,1]
[[{{node MatMul}} = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_Placeholder_0_0, Variable/read)]]
Process finished with exit code 1
答案 0 :(得分:0)
问题是您使用matmul
来乘以一维向量。如果用W
检查W.get_shape()
的形状,它将返回(1,),而它应该是形状为(1,1)的2D矩阵。您只需添加方括号W = tf.Variable([[.3]], tf.float32)
。
也对x
(sess.run(linear_model, {x: [[1, 2, 3, 4]]})
)进行此操作会创建一个(1,4)矩阵。但是,如果将x
和W
相乘,则会出现错误,因为您试图将(1,4)矩阵与(1,1)矩阵相乘,因此它们是不兼容的(第二个值第一矩阵形状中的第一个值必须与第二矩阵形状中的第一个值相同)。相反,您需要对x
进行转置,以便将(4,1)矩阵与(1,1)矩阵相乘。这可以通过使用transpose_a=True
中的matmul
标志来完成。这是可以正确运行的最终代码:
import tensorflow as tf
W = tf.Variable([[.3]], tf.float32)
b = tf.Variable([[-.3]], tf.float32)
x = tf.placeholder(tf.float32)
linear_model = tf.matmul(x, W, transpose_a=True) + b
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(linear_model, {x: [[1, 2, 3, 4]]}))
答案 1 :(得分:0)
好吧,如果添加其他括号,它应该可以工作:
W = tf.Variable([[.3]], tf.float32)
b = tf.Variable([[-.3]], tf.float32)
x = tf.placeholder(tf.float32)
...
print(sess.run(linear_model, {x: [[1],[2],[3],[4]]}))