Skip to main content
 首页 » 编程设计

python之不可散列类型 : 'list' in printing Tensorflow vector

2025年01月19日28lyj

我正在打印 Tensorflow 的矢量和矩阵。它打印 scalar 但显示 vector、Matrix 和 Tensor

的错误

代码如下:

import tensorflow as tf     
scalar = tf.constant([2]) 
vector = tf.constant([3,4,5]) 
Matrix = tf.constant([1,2,3],[4,5,6],[7,8,9]) 
Tensor = tf.constant([ [[1,2,3],[4,5,6],[7,8,9]], 
                       [[1,2,3],[4,5,6],[7,8,9]], 
                       [[1,2,3],[4,5,6],[7,8,9]] ]) 
with tf.Session() as session: 
    result = session.run(scalar) 
    print (result) 
    result = session.run(vector) 
    print (result) 
    result = session.run(Matrix) 
    print (result) 
    result = session.run(Tensor) 
    print (result) 

我收到以下错误:

TypeError: unhashable type: 'list' 

我该如何解决?

请您参考如下方法:

您的第 4 个陈述有误: 使用

Matrix = tf.constant([ [1,2,3],[4,5,6],[7,8,9] ]) 
#                    ^ extra parenthesis here 

代替

Matrix = tf.constant([1,2,3],[4,5,6],[7,8,9])