如何通过keras获得每个图层的输出值?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何通过keras获得每个图层的输出值?相关的知识,希望对你有一定的参考价值。
我想使用keras Lstm获取时间序列功能,然后使用Kmeans的功能。但现在我无法获得图层输出值。如何获取图层输出值?
这是我的网络
Layer (type) Output Shape Param #
lstm_66(LSTM)(无,无,50)10400
lstm_67(LSTM)(无,100)60400
dense_19(密集)(无,1)101
activation_19 (Activation) (None, 1) 0
我想获取lstm_67输出值,我的代码是:
import keras.backend as K
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
sess = tf.Session()
sess.run(tf.global_variables_initializer())
import numpy as np
statesAll=[]
layers = model.layers
print layers[1].output,type(layers[1].output[1]),sess.run(layers[1].output)
结果是:
张量(“lstm_61 / TensorArrayReadV3:0”,shape =(?,100),dtype = float32)
那么,我怎样才能获得图层输出值?
谢谢!
但它不起作用,我的代码是:
def load_data(file_name, sequence_length=10, split=0.8):
df = pd.read_csv(file_name, sep=',', usecols=[1])
data_all = np.array(df).astype(float)
scaler = MinMaxScaler()
data_all = scaler.fit_transform(data_all)
data = []
print len(data_all)
for i in range(len(data_all) - sequence_length - 1):
data.append(data_all[i: i + sequence_length + 1])
reshaped_data = np.array(data).astype('float64')
np.random.shuffle(reshaped_data)
x = reshaped_data[:, :-1]
y = reshaped_data[:, -1]
split_boundary = int(reshaped_data.shape[0] * split)
train_x = x[: split_boundary]
test_x = x[split_boundary:]
train_y = y[: split_boundary]
test_y = y[split_boundary:]
return train_x, train_y, test_x, test_y, scaler
def build_model(n_samples, time_steps, input_dim):
model = Sequential()
model.add(LSTM(input_dim=1, output_dim=50,return_sequences=True))
model.add(LSTM(100, return_sequences=False))
model.add(Dense(output_dim=1))
model.add(Activation('linear'))
model.compile(loss='mse', optimizer='rmsprop')
print(model.layers)
return model
def train_model(train_x, train_y, test_x, test_y):
model = build_model()
model.fit(train_x, train_y, batch_size=128, nb_epoch=30,validation_split=0.1)
return model
train_x, train_y, test_x, test_y, scaler = load_data(file path)
train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1))
test_x = np.reshape(test_x, (test_x.shape[0], test_x.shape[1], 1))
model = train_model(train_x, train_y, test_x, test_y)
from keras import backend as K
layers = model.layers
K.eval(layers[1].output)
答案
keras.backend.eval()
应该这样做。
另一答案
首先,这是一个张量,你需要使用tf. Print ()
方法来查看具体的值。如果您使用Spyder,则不会在控制台中看到此信息。您需要在命令行中执行此程序。
以上是关于如何通过keras获得每个图层的输出值?的主要内容,如果未能解决你的问题,请参考以下文章