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?