Matmul Eror形状

2024-09-26 22:42:54 发布

您现在位置:Python中文网/ 问答频道 /正文

在线性模型中将权重定义为变量

我是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

Tags: runinpypackagestftensorflowlinesite
2条回答

好的,如果您添加其他括号,它应该可以工作:

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]]}))

问题是你在用matmul乘一维向量。如果通过W.get_shape()检查W的形状,它将返回(1,),而它应该是形状(1,1)的二维矩阵。只需添加括号:W = tf.Variable([[.3]], tf.float32),就可以实现这一点。你知道吗

x也这样做(sess.run(linear_model, {x: [[1, 2, 3, 4]]}))会创建一个(1,4)矩阵。但如果将xW相乘,则会出现错误,因为您试图将(1,4)矩阵与(1,1)矩阵相乘,因此它们不兼容(第一个矩阵形状中的第二个值需要与第二个矩阵形状中的第一个值相同)。相反,您需要换位x,以便将(4,1)矩阵与(1,1)矩阵相乘。这可以通过使用matmul中的transpose_a=True标志来实现。下面是最后一段运行时不会出错的代码:

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]]}))

相关问题 更多 >

    热门问题