python - How can I multiply a vector and a matrix in tensorflow without reshaping? -


this:

import numpy np = np.array([1, 2, 1]) w = np.array([[.5, .6], [.7, .8], [.7, .8]])  print(np.dot(a, w)) # [ 2.6  3. ] # plain nice old matrix multiplication n x (n, m) -> m  import tensorflow tf  = tf.constant(a, dtype=tf.float64) w = tf.constant(w)  tf.session() sess:     print(tf.matmul(a, w).eval()) 

results in:

c:\_\python35\python.exe c:/users/mrd/.pycharm2017.1/config/scratches/scratch_31.py [ 2.6  3. ] # bunch of errors in windows... traceback (most recent call last):   file "c:\_\python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 671, in _call_cpp_shape_fn_impl     input_tensors_as_shapes, status)   file "c:\_\python35\lib\contextlib.py", line 66, in __exit__     next(self.gen)   file "c:\_\python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status     pywrap_tensorflow.tf_getcode(status)) tensorflow.python.framework.errors_impl.invalidargumenterror: shape must rank 2 rank 1 'matmul' (op: 'matmul') input shapes: [3], [3,2].  during handling of above exception, exception occurred:  traceback (most recent call last):   file "c:/users/mrd/.pycharm2017.1/config/scratches/scratch_31.py", line 14, in <module>     print(tf.matmul(a, w).eval())   file "c:\_\python35\lib\site-packages\tensorflow\python\ops\math_ops.py", line 1765, in matmul     a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)   file "c:\_\python35\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 1454, in _mat_mul     transpose_b=transpose_b, name=name)   file "c:\_\python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 763, in apply_op     op_def=op_def)   file "c:\_\python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2329, in create_op     set_shapes_for_outputs(ret)   file "c:\_\python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1717, in set_shapes_for_outputs     shapes = shape_func(op)   file "c:\_\python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1667, in call_with_requiring     return call_cpp_shape_fn(op, require_shape_fn=true)   file "c:\_\python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 610, in call_cpp_shape_fn     debug_python_shape_fn, require_shape_fn)   file "c:\_\python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 676, in _call_cpp_shape_fn_impl     raise valueerror(err.message) valueerror: shape must rank 2 rank 1 'matmul' (op: 'matmul') input shapes: [3], [3,2].  process finished exit code 1 

(not sure why same exception raised inside handling)

the solution suggested in tensorflow exception matmul reshaping vector matrix leads needlessly complicated code - there still no other way multiply vector matrix?

incidentally using expand_dims (as suggested in link above) default arguments raises valueerror - that's not mentioned in docs , defeats purpose of having default argument.

matmul coded rank 2 or greater tensors. not sure why honest numpy has such allows matrix vector multiplication well.

import numpy np = np.array([1, 2, 1]) w = np.array([[.5, .6], [.7, .8], [.7, .8]])  print(np.dot(a, w)) # [ 2.6  3. ] # plain nice old matix multiplication n x (n, m) -> m print(np.sum(np.expand_dims(a, -1) * w , axis=0)) # equivalent result [2.6, 3]  import tensorflow tf  = tf.constant(a, dtype=tf.float64) w = tf.constant(w)  tf.session() sess:   # produce same result numpy above   print(tf.matmul(tf.expand_dims(a,0), w).eval())   print((tf.reduce_sum(tf.multiply(tf.expand_dims(a,-1), w), axis=0)).eval())   print((tf.reduce_sum(tf.multiply(a, tf.transpose(w)), axis=1)).eval())    # note tf.multiply equivalent "*"   print((tf.reduce_sum(tf.expand_dims(a,-1) * w, axis=0)).eval())   print((tf.reduce_sum(a * tf.transpose(w), axis=1)).eval()) 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -