Issue
This is probably quite a basic tensorflow/keras question but it is I can't seem to find it in the docs. I'm looking to retrieve the output of the hidden layer as numerical values for use in subsequent calculations. Here's the model
from io import StringIO
import pandas as pd
import numpy as np
import keras
data_str = """
ti,z1,z2
0.0,1.000,0.000
0.1,0.606,0.373
0.2,0.368,0.564
0.3,0.223,0.647
0.4,0.135,0.669
0.5,0.082,0.656
0.6,0.050,0.624
0.7,0.030,0.583
0.8,0.018,0.539
0.9,0.011,0.494
1.0,0.007,0.451"""
data = pd.read_csv(StringIO(data_str), sep=',')
wd = r'/path/to/working/directory'
model_filename = os.path.join(wd, 'example1_with_keras.h5')
RUN = True
if RUN:
model = keras.Sequential()
model.add(keras.layers.Dense(3, activation='tanh', input_shape=(1, )))
model.add(keras.layers.Dense(2, activation='tanh'))
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(data['ti'].values, data[['z1', 'z2']].values, epochs=30000)
model.save(filepath=model_filename)
else:
model = keras.models.load_model(model_filename)
outputs = model.layers[1].output
print(outputs)
This prints the following:
>>> Tensor("dense_2/Tanh:0", shape=(?, 2), dtype=float32)
How can I get the output as a np.array
rather than a Tensor
object?
Solution
Using model.layer[1].output
does not produce an output, it simply returns the tensor definition of the output. In order to actually produce an output, you need to run your data through the model and specify model.layer[1].output
as the output.
You can do this by using tf.keras.backend.function
(documentation), which will return Numpy arrays. A similar question to yours can be found here.
The following should work for your example if you only want the output from model.layers[1].output
and if you convert your data
to a Numpy array for input:
from keras import backend as K
outputs = [model.layers[1].output]
functor = K.function([model.input, K.learning_phase()], outputs)
layer_outs = functor([data, 1.])
Answered By - Luke DeLuccia
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.