【问题标题】:Assign a tensorflow variable to an element in an array将张量流变量分配给数组中的元素
【发布时间】:2017-09-04 05:31:30
【问题描述】:

我是 tensorflow 的新手,当我使用它时。我遇到了一些问题

import numpy as np

t_p2 = tf.Variable(5., dtype=tf.float32)
spar_f = tf.exp(t_p2)

A = np.array(3)
A[0] = spar_f

然后出现错误:

ValueError: setting an array element with a sequence.

一般来说,如果我想将 tensorflow 对象分配给 numpy 数组中的元素。 最好的方法是什么?

谢谢

【问题讨论】:

    标签: python arrays numpy tensorflow


    【解决方案1】:

    尝试以下方法:

    import numpy as np
    import tensorflow as tf
    
    t_p2 = tf.Variable(5., dtype=tf.float32)
    spar_f = tf.exp(t_p2)
    
    A = np.array([3])
    with tf.Session() as sess:
        sess.run(t_p2.initializer)
        A[0] = sess.run(spar_f)
    
    print(A)
    

    请注意,您在原始示例中确实将 A 转换为数组。

    那么你错过了什么? first part of the getting started tutorial 解释范式的地方,在 tensorflow 中首先定义一个图。构建图表后,您可以在会话中使用该图表来运行计算。我在上面的例子中做了什么,运行计算并将结果存储在一个 numpy 数组中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      相关资源
      最近更新 更多