简体   繁体   English

如何从tensorflow einsum结果中获取numpy数组?

[英]How can I get the numpy array out of the tensorflow einsum result?

y = tf.einsum('aij,jk->aik', x, W) + b

y.shape returns the following: y.shape返回以下内容:

tf.Tensor 'text-representation/add:0' shape=(?, 80, 256) dtype=float32

How can I get the 80x256 array into the numpy array? 如何将80x256数组放入numpy数组? I am new to tensorflow and struggling. 我是tensorflow和挣扎的新手。 Thanks. 谢谢。

They are two ways to approach this: 有两种方法可以解决此问题:

1). 1)。 Execute the Tensor within a Session. 在会话中执行张量。

y = tf.einsum('aij,jk->aik', x, W) + b

sess = tf.Session()
print(sess.run(y))

# store the numpy array
my_arr = sess.run(y)
my_arr.shape

sess.close()  # close the session object

2). 2)。 Use Eager Execution for immediate execution. 使用急切执行立即执行。

# enable eager execution
tf.enable_eager_execution()

y = tf.einsum('aij,jk->aik', x, W) + b
print(y)

Note, however, that to use Eager Execution it must be enabled before any other operation is carried out on the terminal. 但是请注意,要使用“急切执行”,必须在终端上执行任何其他操作之前将其启用。 Moreso, when Eager Execution is enabled, it cannot be disabled unless the terminal is restarted. 此外,启用“紧急执行”后,除非重新启动终端,否则无法将其禁用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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