【问题标题】:How can I get every layers output value with keras?如何使用 keras 获得每一层的输出值?
【发布时间】:2018-05-18 08:44:59
【问题描述】:

我想使用 keras Lstm 获取时间序列特征,然后将这些特征用于 Kmeans。但现在我无法获得图层输出值。如何获取图层输出值?

这是我的 lstm 网络


层(类型)输出形状参数#

lstm_66 (LSTM)(无,无,50)10400


lstm_67 (LSTM) (无, 100) 60400


dense_19(密集)(无,1)101


activation_19(激活)(无,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)

结果是:

Tensor("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)

【问题讨论】:

    标签: neural-network keras time-series lstm layer


    【解决方案1】:

    在 TensorFlow 2.x 中,您可以这样做:

    from tensorflow.python.keras import backend as K
    
    model = build_model() 
    # lstm_67 is the second layer.
    lstm = K.function([model.layers[0].input], [model.layers[1].output])
    lstm_output = lstm([test_x])[0]
    

    【讨论】:

      【解决方案2】:

      keras.backend.eval() 应该可以。

      查看文档herehere

      【讨论】:

      • 谢谢!但它不起作用,我将代码添加到原始问题中,请帮助我。我怎样才能获得模型erery层输出特征值。
      • 也许你可以看看这个link
      • 感谢您的帮助!
      【解决方案3】:

      首先,这是一个张量,需要使用tf. Print ()方法查看具体值。如果您使用 Spyder,您将不会在控制台中看到此信息。你需要在命令行中执行这个程序。

      【讨论】:

      • 看不懂你的回答,能不能说的详细一点?谢谢!
      • 你可以试试这个代码:tf.Print(layers[1].output).
      • 谢谢!代码解决了我的问题: from keras import backend as K def get_activations(model, layer, X_batch): get_activations = K.function([model.layers[0].input, K.learning_phase()], [model.layers[ layer].output,]) 激活 = get_activations([X_batch,0]) 返回激活
      • 恭喜您解决了问题。我也会学习代码。
      猜你喜欢
      • 2017-06-02
      • 1970-01-01
      • 2019-03-28
      • 2021-11-10
      • 1970-01-01
      • 2020-03-08
      • 2020-12-27
      • 1970-01-01
      • 2019-11-18
      相关资源
      最近更新 更多