我正在使用Windows 10,Python 3.5和tensorflow 1.1.0。我有以下脚本:
import tensorflow as tf
import tensorflow.contrib.keras.api.keras.backend as K
from tensorflow.contrib.keras.api.keras.layers import Dense
tf.reset_default_graph()
init = tf.global_variables_initializer()
sess = tf.Session()
K.set_session(sess) # Keras will use this sesssion to initialize all variables
input_x = tf.placeholder(tf.float32, [None, 10], name='input_x')
dense1 = Dense(10, activation='relu')(input_x)
sess.run(init)
dense1.get_weights()
我收到错误:
AttributeError: 'Tensor' object has no attribute 'weights'
我在做什么错,如何获得
dense1
的权重?我看过this和this这样的帖子,但是我仍然无法使它工作。 最佳答案
如果您写:dense1 = Dense(10, activation='relu')(input_x)
那么dense1
不是图层,而是图层的输出。该层是Dense(10, activation='relu')
所以看来您的意思是:
dense1 = Dense(10, activation='relu')
y = dense1(input_x)
这是完整的代码段:
import tensorflow as tf
from tensorflow.contrib.keras import layers
input_x = tf.placeholder(tf.float32, [None, 10], name='input_x')
dense1 = layers.Dense(10, activation='relu')
y = dense1(input_x)
weights = dense1.get_weights()