Issue
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import LSTM, Conv1D, Dense
import numpy as np
#Create test data
X = np.array(list(range(100)))
X = np.reshape(X,(1,100,1))
print(X.shape)
#Model A
model_a = Sequential()
model_a.add(Conv1D(filters=1, kernel_size=10, strides=10, kernel_initializer = tf.keras.initializers.ones, use_bias = False, activation=None, padding='valid',input_shape=(100,1)))
model_a.add(Conv1D(filters=1, kernel_size=10, strides=10, kernel_initializer = tf.keras.initializers.ones, use_bias = False, activation=None, padding='valid',input_shape=(10,1)))
print(model_a.summary())
#Model A predict
predict_a = model_a.predict(X)
print(predict_a)
print(predict_a.shape)
#Model B
model_b = Sequential()
model_b.add(Conv1D(filters=1, kernel_size=10, strides=10, kernel_initializer = tf.keras.initializers.ones, use_bias = False, activation=None, padding='valid',input_shape=(100,1)))
#Make predict and add node after that
tmpPredict = model_b.predict(X)
#
model_b.add(Conv1D(filters=1, kernel_size=10, strides=10, kernel_initializer = tf.keras.initializers.ones, use_bias = False, activation=None, padding='valid',input_shape=(10,1)))
print(model_a.summary())
#Model B predict
predict_b = model_b.predict(X)
print(predict_b)
print(predict_b.shape)
The Model A seem fine, the final output shape is (1,1,1). But Model B seem cannot add the new node after the predict() function called. Why?
Solution
Tensorflow automatically creates a static computation graph once the model is built (building happens when you first call the model). This is the default behavior according to the documentation of tf.keras.Sequential
:
By default, we will attempt to compile your model to a static graph to deliver the best execution performance.
To instead run the model eagerly, you can disable this graph building by calling tf.config.run_functions_eagerly(True)
before calling model.predict
. Running the model eagerly should allow you to add layers after model.predict
has been called.
For more information on the static computation graphs, refer to https://www.tensorflow.org/guide/intro_to_graphs
Answered By - Chillston
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.