1
votes

I want use keras Lstm to get the time series features, then use the features to Kmeans. But now I can not get the layers output values. How can I get the layers output values?

This is my lstm network


Layer (type) Output Shape Param #

lstm_66 (LSTM) (None, None, 50) 10400


lstm_67 (LSTM) (None, 100) 60400


dense_19 (Dense) (None, 1) 101


activation_19 (Activation) (None, 1) 0

I want to get the lstm_67 output values,my code is:

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)

and the result is:

Tensor("lstm_61/TensorArrayReadV3:0", shape=(?, 100), dtype=float32)

So, how can I get the layers output value?

Thanks!

But it not work,my code is:

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)
3

3 Answers

2
votes

In TensorFlow 2.x, you can do like this:

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]
1
votes

keras.backend.eval() should do.

Look at the documentation here and here

1
votes

First of all, this is a tensor, you need to use the tf. Print () method to see the specific value. If you use Spyder, you will not see this information in the console. You need to execute this program in the command line.