简体   繁体   中英

How to convert tensor to numpy array the result of matmul

I want to convert fc1 tensor to numpy array. How to get it?

fc1 = tf.reshape(conv1, [-1, weights['wd1'].get_shape().as_list()[0]])
fc1 = tf.matmul(fc1, weights['wd1'])

Any tensor returned by Session.run or eval is a NumPy array. So tensor to numpy array you can simply run .eval() on the transformed tensor.

ie:

 sess = tf.Session()
 fc1.eval(session=sess)

It causes this error

    fc1 = tf.reshape(conv1, [-1, weights['wd1'].get_shape().as_list()[0]])
    fc1 = tf.matmul(fc1, weights['wd1'])
    sess = tf.Session()
    fc1.eval(session=sess)

    Traceback (most recent call last):
  File "C:/Users/ybrot/OneDrive/바탕 화면/QCNN/QCNN.py", line 150, in <module>
    logits = conv_net(X, weights, keep_prob)
  File "C:/Users/ybrot/OneDrive/바탕 화면/QCNN/QCNN.py", line 114, in conv_net
    fc1.eval(session=sess)
  File "C:\Users\ybrot\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 731, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "C:\Users\ybrot\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 5579, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "C:\Users\ybrot\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 950, in run
    run_metadata_ptr)
  File "C:\Users\ybrot\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1173, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Users\ybrot\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1350, in _do_run
    run_metadata)
  File "C:\Users\ybrot\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1370, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,256]
     [[node Placeholder (defined at /Users/ybrot/OneDrive/바탕 화면/QCNN/QCNN.py:54) ]]
     [[MatMul/_1]]
  (1) Invalid argument: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,256]
     [[node Placeholder (defined at /Users/ybrot/OneDrive/바탕 화면/QCNN/QCNN.py:54) ]]
0 successful operations.
0 derived errors ignored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM