Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在tensorflow中将张量的形状表示为数组_Python_Tensorflow_Machine Learning_Computer Vision - Fatal编程技术网

Python 在tensorflow中将张量的形状表示为数组

Python 在tensorflow中将张量的形状表示为数组,python,tensorflow,machine-learning,computer-vision,Python,Tensorflow,Machine Learning,Computer Vision,我有一个保存的模型,我想从中获得应用于最终层的最终权重。我已经加载了图,知道张量在哪里,但是我不能得到张量作为数组的形状。我知道阵列的形状是2048x6。如何获得像这样的实际值 [[1,2,3],[1,2,3]...]. 谢谢 这是我的密码 import tensorflow as tf saver = tf.train.import_meta_graph('_retrain_checkpoint.meta') graph = tf.get_default_graph() tensor =

我有一个保存的模型,我想从中获得应用于最终层的最终权重。我已经加载了图,知道张量在哪里,但是我不能得到张量作为数组的形状。我知道阵列的形状是2048x6。如何获得像这样的实际值
[[1,2,3],[1,2,3]...]. 谢谢

这是我的密码

import tensorflow as tf

saver = tf.train.import_meta_graph('_retrain_checkpoint.meta')
graph = tf.get_default_graph()

tensor = tf.get_default_graph().get_tensor_by_name("final_retrain_ops/weights/final_weights:0")

print(tensor)
print(tf.TensorShape(tensor.get_shape()).as_list()




>>>Tensor("final_retrain_ops/weights/final_weights:0", shape=(2048, 6), dtype=float32_ref)
>>>(2048, 6)

要打印权重张量的值,可以执行以下操作:

with tf.Session() as sess:
    print( sess.run( tensor ) )
计算it参数中的张量,这意味着它将打印值

然而,有一点问题是,您的代码只加载图的结构(
tf.train.import\u meta\u graph(“\u retain\u checkpoint.meta”)
),而不是预训练的值。因此,您会得到一个错误,即您试图使用未初始化的值

您需要有类似于:

saver.restore(sess,tf.train.latest_checkpoint('./'))
要加载它,就在定义了
sess
之后,当然,您需要指向正确的检查点目录,而不是
/

比如说:

with tf.Session() as sess:
    saver.restore(sess,tf.train.latest_checkpoint('./'))
    print( sess.run( tensor ) )

你的问题有点不清楚<代码>打印(tensor.get_shape().as_list())将为您提供张量的形状。但从你的问题来看,不清楚你是否想要这些值(即本例中的权重)?很抱歉,我想要这些值。更新答案作为对你评论的回应。