我想在tensorflow中打印一个张量值,所以我在网上阅读了很多,但我仍然无法使它工作,如果你能理解我做错了什么我会很高兴,我想这会帮助我理解张量更好 .

sess = tf.InteractiveSession()

with tf.name_scope("embedding"):
  #sess = tf.InteractiveSession() #tried to insert also in here
  embeddings = tf.gather(self.shared_weights, x)
  embeddings *= self.hidden_size ** 0.5
  print(embeddings.eval())#this is what I want to print

  padding = model_utils.get_padding(x)
  embeddings *= tf.expand_dims(1 - padding, -1)
  return embeddings

sess.close()

我收到这个错误:

FailedPreconditionError(参见上面的回溯):尝试使用未初始化的值模型/ Transformer / embedding_shared_weights / embedding_and_softmax / weights [[Node:model / Transformer / embedding_shared_weights / embedding_and_softmax / weights / read = IdentityT = DT_FLOAT,class = [“loc:@ model / Transformer / embedding_shared_weights / embedding_and_softmax / weights“], device =”/ job:localhost / replica:0 / task:0 / device:CPU:0“]]

这是我尝试的另一件事:

with tf.name_scope("embedding"):

  embeddings = tf.gather(self.shared_weights, x)
  x = tf.get_variable('x', [tf.get_shape(embeddings)[0], tf.get_shape(embeddings)[1], tf.get_shape(embeddings)[2]])
  x = tf.Print(embeddings, [embeddings])
  sess = tf.InteractiveSession()
  sess.run(x)
  # Scale embedding by the sqrt of the hidden size
  embeddings *= self.hidden_size ** 0.5
  print(embeddings.eval())
  # Create binary array of size [batch_size, length]
  # where 1 = padding, 0 = not padding
  padding = model_utils.get_padding(x)
  # Set all padding embedding values to 0
  embeddings *= tf.expand_dims(1 - padding, -1)
  return embeddings

我收到这个错误:

TypeError:int()参数必须是字符串,类字节对象或数字,而不是'Tensor'

我尝试了一些变化,但无法做任何事情,如果你能帮助理解,我会很高兴,我会很高兴 .

非常感谢!