当前位置:   article > 正文

x = tf.constant([[0.7, 0.9]])与x = tf.constant([0.7, 0.9])

x = tf.constant

下面这段代码:

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].


今天知道了原因,少写了一对括号。

也许,一对括号表示的是列表,两对括号才表示矩阵。矩阵要与矩阵相乘,导致了前面的错误。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/464732
推荐阅读
相关标签
  

闽ICP备14008679号