I am new to neural networks and I am working on prediction from images. I would like to predict 101 data points from an image. The images are made from data measured by sensors . I have tried the following code, but when I predict, I always get the same prediction. Any idea how to solve this problem? I also tried with more epochs, different layers, different activation functions and got the same problem.
Trainingfolder = 'C:\Users\sozan\Documents\SmartLocomotion\Dataset\Dataset1\Trainingset'
Testfolder = r'C:\Users\sozan\Documents\Smart Locomotion\Dataset\Dataset1\Testset'
image_files = [os.path.join(Trainingfolder, f) for f in os.listdir(Trainingfolder) if f.endswith('.jpeg')]
plt.figure()
for i in range(5):
imgfile = random.choice(image_files)
img = image.imread(imgfile)
outname = imgfile[0:-5] + ".mat"
outfile = scipy.io.loadmat(outname, squeeze_me=True,struct_as_record=False)
output = outfile['output'].AnkleFlexExt
ax=plt.subplot(2,5,i+1)
plt.imshow(img)
ax=plt.subplot(2,5,i+1+5)
plt.plot(output)
dataset : the image is made from sensors data and the curve represent the output data that I would like to predict
def create_dataset(datafolder):
image_files = [os.path.join(datafolder, f) for f in os.listdir(datafolder) if f.endswith('.jpeg')]
img_data_array = []
output_data = []
for imgfile in image_files:
imag = image.imread(imgfile)
imag = np.array(imag)
imag = imag.astype('float64')
imag /= 255
img_data_array.append(imag)
outname = imgfile[0:-5] + ".mat"
outfile = scipy.io.loadmat(outname, squeeze_me=True,struct_as_record=False)
output = outfile['output'].AnkleFlexExt
output_data.append(output)
return img_data_array, output_data
img_Trainingdata, output_Trainingdata = create_dataset(Trainingfolder)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(64, kernel_size=(3,3), activation='relu', input_shape=(101,9,3)))
model.add(tf.keras.layers.MaxPool2D(pool_size=(2, 2), padding='same'))
model.add(tf.keras.layers.Conv2D(128, kernel_size=(3,3), activation='relu'))
model.add(tf.keras.layers.MaxPool2D(pool_size=(2, 2)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(3000, activation = 'relu'))
model.add(tf.keras.layers.Dense(101))
adam = tf.keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False);
model.compile(optimizer=adam, loss='mse', metrics=["mse"])
model.summary()
Layer (type)
Output Shape
Param #
conv2d_2 (Conv2D)
(None, 99, 7, 64)
1792
max_pooling2d (MaxPooling2D)
(None, 50, 4, 64)
0
conv2d_3 (Conv2D)
(None, 48, 2, 128)
73856
max_pooling2d_1 (MaxPooling2D)
(None, 24, 1, 128)
0
flatten_1 (Flatten)
(None, 3072)
0
dense_2 (Dense)
(None, 3000)
9219000
dense_3 (Dense)
(None, 101)
303101
Total params: 9,597,749
Trainable params: 9,597,749
Non-trainable params: 0
history = model.fit(x=np.array(img_Trainingdata, np.float64), y=np.array(output_Trainingdata, np.float64), epochs=1)
36/36 [==============================] - 2s 55ms/step - loss: 0.0639 - mse: 0.0639
def load_Testdata(dataname):
img_data_array = []
imag = image.imread(imgfile)
imag = np.array(imag)
imag = imag.astype('float64')
imag /= 255
img_data_array.append(imag)
return img_data_array
image_test = [os.path.join(Testfolder, f) for f in os.listdir(Testfolder) if f.endswith('.jpeg')]
plt.figure()
for itest in image_test:
outname = itest[0:-5] + ".mat"
outfile = scipy.io.loadmat(outname, squeeze_me=True,struct_as_record=False)
output = outfile['output'].AnkleFlexExt
plt.plot(output, color='blue', label='Measured')
for itest in image_test:
img_test = load_Testdata(itest)
pred = model.predict (x=np.array(img_test, np.float64))
pred = pred[0]
plt.plot(pred, color='red', label='Predicted')
plt.title('Ankle Flex-Ext Moment')
plt.ylabel('Nm.kg$^{-1}$')
plt.xlabel('% cycle')
plt.show()
Related
How to fix the error "Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 150528 but received input with shape [224, 672]".
I'd problem to build sequantial model. Hope you all will help me on this time.
def decode_csv(csv_row):
record_defaults = ["path", "flower"]
filename, label_string = tf.io.decode_csv(csv_row, record_defaults)
img = read_and_decode(filename, [IMG_HEIGHT, IMG_WIDTH])
return img, label_string
train_dataset (tf.data.TextLineDataset("/home/pi/Downloads/ml_code/train_set.csv").map(decode_csv)).take(500)
eval_dataset = (tf.data.TextLineDataset("/home/pi/Downloads/ml_code/eval_set.csv").map(decode_csv)).take(50)
model =tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape = (IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS)),
tf.keras.layers.Dense(len(CLASS_NAME), activation = "softmax")
])
model.compile(optimizer="adam",
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),
metrics = ['accuracy'])
model.summary()
tf.keras.utils.plot_model(model, show_shapes = True, show_layer_names = False, to_file = "model.jpg")
history = model.fit(train_dataset, validation_data = eval_dataset, epochs = 10)
model.save("first")
json.dump(history.history, open("First_History", "w"))
Try something like this:
import pandas as pd
# Create dummy data
tf.keras.utils.save_img('image1.png', tf.random.normal((64, 64, 3)))
tf.keras.utils.save_img('image2.png', tf.random.normal((64, 64, 3)))
tf.keras.utils.save_img('image3.png', tf.random.normal((64, 64, 3)))
tf.keras.utils.save_img('image4.png', tf.random.normal((64, 64, 3)))
tf.keras.utils.save_img('image5.png', tf.random.normal((64, 64, 3)))
df = pd.DataFrame(data= {'path': ['/content/image1.png', '/content/image2.png', '/content/image3.png', '/content/image4.png', '/content/image5.png'],
'label': ['0', '1', '2', '3', '2']})
df.to_csv('data.csv', index=False)
Preprocess data and train:
import tensorflow as tf
def decode_csv(csv_row):
record_defaults = ["path", "label"]
filename, label_string = tf.io.decode_csv(csv_row, record_defaults)
img = tf.io.decode_png(tf.io.read_file(filename), channels=3)
return img, tf.strings.to_number(label_string, out_type=tf.int32)
# Skip header row.
train_dataset = tf.data.TextLineDataset("/content/data.csv").skip(1).map(decode_csv).batch(2)
model =tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape = (64, 64, 3)),
tf.keras.layers.Dense(4, activation = "softmax")
])
model.compile(optimizer="adam",
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),
metrics = ['accuracy'])
model.summary()
tf.keras.utils.plot_model(model, show_shapes = True, show_layer_names = False, to_file = "model.jpg")
history = model.fit(train_dataset, epochs = 2)
model.save("first")
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten_2 (Flatten) (None, 12288) 0
dense_2 (Dense) (None, 4) 49156
=================================================================
Total params: 49,156
Trainable params: 49,156
Non-trainable params: 0
_________________________________________________________________
Epoch 1/2
3/3 [==============================] - 1s 62ms/step - loss: 623.7551 - accuracy: 0.4000
Epoch 2/2
3/3 [==============================] - 0s 7ms/step - loss: 1710.6586 - accuracy: 0.2000
INFO:tensorflow:Assets written to: first/assets
My Final Fix Code is here:
def read_and_decode(filename, reshape_dims):
img = tf.io.read_file(filename)
# Range [0, 255]
img = tf.image.decode_jpeg(img, channels = 3)
#convert into range[0, 1] for ml flexible
img = tf.image.convert_image_dtype(img, tf.float32)
return img
def decode_csv(csv_row):
record_defaults = ["path", "flower"]
filenames, label_string = tf.io.decode_csv(csv_row, record_defaults, field_delim = ",")
img = read_and_decode(filenames, [IMG_HEIGHT, IMG_WIDTH])
return img, tf.argmax(tf.cast(label_string == CLASS_NAME, tf.int32))
train_dataset = tf.data.TextLineDataset("/home/pi/Downloads/ml_code/train_set.csv").skip(1).map(decode_csv).batch(2)
eval_dataset = tf.data.TextLineDataset("/home/pi/Downloads/ml_code/eval_set.csv").skip(1).map(decode_csv).batch(2)
model =tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape = (IMG_HEIGHT, IMG_WIDTH, 3)),
tf.keras.layers.Dense(len(CLASS_NAME), activation = "softmax")
])
model.compile(optimizer="adam",
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),
metrics = ['accuracy'])
model.summary()
tf.keras.utils.plot_model(model, show_shapes = True, show_layer_names = False, to_file = "model.jpg")
history = model.fit(train_dataset, validation_data = eval_dataset, epochs = 2)
model.save("first")
json.dump(history.history, open("First_History", "w"))
All credits should go to AloneTogether, Thanks for your help ^_^.
I have been debugging this error for the past week and I'm not sure why my code is not working.
We have a custom environment and our reinforcement learning problem is taking a 512x512 image and deciding if we should do action 1 or action 2.
env = customEnv()
nb_actions = env.action_space.n # 2 options
shape = env.observation_space.shape
pool_size = 2
input_shape = (512, 512, 1) # 1 channel, grayscale image
model = Sequential()
model.add(Convolution2D(32, 3, padding="same", input_shape=input_shape))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))
model.add(Convolution2D(64, 2, padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))
model.add(Convolution2D(64, 2, padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(nb_actions))
model.add(Activation('linear'))
print(model.summary())
memory = SequentialMemory(limit=1000000, window_length=WINDOW_LENGTH)
policy = BoltzmannQPolicy()
dqn = DQNAgent(model=model, nb_actions=nb_actions, policy=policy, memory=memory,
nb_steps_warmup=50000, gamma=.99, target_model_update=10000,
train_interval=4, delta_clip=1.)
dqn.compile(Adam(learning_rate=.00025), metrics=['mae'])
dqn.fit(env, nb_steps=50000, visualize=False, verbose=2)
dqn.save_weights(f'dqn_CTEnv_weights.h5f', overwrite=True)
dqn.test(env, nb_episodes=5, visualize=False)
Full error log:
Traceback (most recent call last):
File "DQN_CT.py", line 60, in <module>
dqn.fit(env, nb_steps=50000, visualize=False, verbose=2)
File "/home/anaconda3/envs/lib/python3.7/site-packages/rl/core.py", line 168, in fit
action = self.forward(observation)
File "/home/anaconda3/envs/lib/python3.7/site-packages/rl/agents/dqn.py", line 224, in forward
q_values = self.compute_q_values(state)
File "/home/anaconda3/envs/lib/python3.7/site-packages/rl/agents/dqn.py", line 68, in compute_q_values
q_values = self.compute_batch_q_values([state]).flatten()
File "/home/anaconda3/envs/lib/python3.7/site-packages/rl/agents/dqn.py", line 63, in compute_batch_q_values
q_values = self.model.predict_on_batch(batch)
File "/home/anaconda3/envs/python3.7/site-packages/tensorflow/python/keras/engine/training_v1.py", line 1201, in predict_on_batch
x, extract_tensors_from_dataset=True)
File "/home/anaconda3/envs/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_v1.py", line 2334, in _standardize_user_data
batch_size=batch_size)
File "/home/anaconda3/envs/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_v1.py", line 2361, in _standardize_tensors
exception_prefix='input')
File "/home/anaconda3/envs/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_utils.py", line 574, in standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking input: expected permute_input to have 4 dimensions, but got array with shape (1, 4)
I have looked at a lot of other posts regarding this error and most seems to point out that the input shape needs to be 3D (width, height, channel) which doesn't seem to be working for us. We also tried (batch size (window_length), width, height, channel), but doing so gives us another error ValueError: Input 0 of layer permute is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [None, 4, 512, 512, 1]
Any help on this problem would really be appreciated!
Tried your model architecture with different dataset with little modifications .
Working sample code
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
import pathlib
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
batch_size = 32
img_height = 512
img_width = 512
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
for image_batch, labels_batch in train_ds:
print(image_batch.shape)
print(labels_batch.shape)
break
input_shape = (180, 180, 3) # 3 channel, RGB image
model = Sequential()
model.add(tf.keras.layers.Conv2D(32, 3, activation= 'relu',padding="same", input_shape=input_shape))
#model.add(Activation("relu"))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(64, 2, activation= 'relu',padding="same"))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(64, 2, activation= 'relu',padding="same"))
#model.add(Activation("relu"))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(64,activation= 'relu'))
#model.add(Activation('relu'))
model.add(tf.keras.layers.Dense(32,activation= 'linear'))
#model.add(Activation('linear'))
print(model.summary())
Output
Model: "sequential_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_7 (Conv2D) (None, 180, 180, 32) 896
max_pooling2d_6 (MaxPooling (None, 90, 90, 32) 0
2D)
conv2d_8 (Conv2D) (None, 90, 90, 64) 8256
max_pooling2d_7 (MaxPooling (None, 45, 45, 64) 0
2D)
conv2d_9 (Conv2D) (None, 45, 45, 64) 16448
max_pooling2d_8 (MaxPooling (None, 22, 22, 64) 0
2D)
flatten_2 (Flatten) (None, 30976) 0
dense_4 (Dense) (None, 64) 1982528
dense_5 (Dense) (None, 32) 2080
=================================================================
Total params: 2,010,208
Trainable params: 2,010,208
Non-trainable params: 0
I wrote the following model fn:
from tensorflow.keras.layers import Dense, LSTM, Dropout, Input, BatchNormalization
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
import tensorflow_addons as tfa
import tensorflow as tf
def get_model(num_features, output_size, output_bias=None):
output_bias = tf.keras.initializers.Constant(output_bias)
opt = Adam(learning_rate=0.0008)
inputs = Input(shape=[None, num_features], dtype=tf.float32, ragged=True)
layers = LSTM(32, activation='tanh')(
inputs.to_tensor(), mask=tf.sequence_mask(inputs.row_lengths()))
layers = BatchNormalization()(layers)
layers = Dropout(0.05)(layers)
layers = Dense(32, activation='relu')(layers)
layers = BatchNormalization()(layers)
layers = Dropout(0.05)(layers)
layers = Dense(32, activation='relu')(layers)
layers = BatchNormalization()(layers)
layers = Dropout(0.05)(layers)
layers = Dense(output_size, activation='sigmoid',
bias_initializer=output_bias)(layers)
model = Model(inputs, layers)
model.compile(loss=tf.keras.losses.BinaryCrossentropy(), optimizer=opt, metrics=[tfa.metrics.F1Score(num_classes=2)])
model.summary()
return model
here is the model summary:
Model: "model_5"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_6 (InputLayer) [(None, None, 11)] 0
__________________________________________________________________________________________________
input.row_lengths_5 (InstanceMe (None,) 0 input_6[0][0]
__________________________________________________________________________________________________
input.to_tensor_5 (InstanceMeth (None, None, 11) 0 input_6[0][0]
__________________________________________________________________________________________________
tf.sequence_mask_5 (TFOpLambda) (None, None) 0 input.row_lengths_5[0][0]
__________________________________________________________________________________________________
lstm_5 (LSTM) (None, 32) 5632 input.to_tensor_5[0][0]
tf.sequence_mask_5[0][0]
__________________________________________________________________________________________________
batch_normalization_15 (BatchNo (None, 32) 128 lstm_5[0][0]
__________________________________________________________________________________________________
dropout_15 (Dropout) (None, 32) 0 batch_normalization_15[0][0]
__________________________________________________________________________________________________
dense_15 (Dense) (None, 32) 1056 dropout_15[0][0]
__________________________________________________________________________________________________
batch_normalization_16 (BatchNo (None, 32) 128 dense_15[0][0]
__________________________________________________________________________________________________
dropout_16 (Dropout) (None, 32) 0 batch_normalization_16[0][0]
__________________________________________________________________________________________________
dense_16 (Dense) (None, 32) 1056 dropout_16[0][0]
__________________________________________________________________________________________________
batch_normalization_17 (BatchNo (None, 32) 128 dense_16[0][0]
__________________________________________________________________________________________________
dropout_17 (Dropout) (None, 32) 0 batch_normalization_17[0][0]
__________________________________________________________________________________________________
dense_17 (Dense) (None, 1) 33 dropout_17[0][0]
==================================================================================================
Total params: 8,161
Trainable params: 7,969
Non-trainable params: 192
__________________________________________________________________________________________________
And here are the shapes of my data:
print(train_x.shape,train_y.shape)
print(val_x.shape,val_y.shape)
(52499, None, 11) (52499,)
(17500, None, 11) (17500,)
When trying to fit my model, I get the following error:
model.fit(train_x, train_y, epochs=300, batch_size=500, validation_data=(val_x, val_y))
ValueError: Dimension 0 in both shapes must be equal, but are 2 and 1. Shapes are [2] and [1].
I can't understand what is wrong with the shapes.
Your model seems fine. The problem is that you are running into an open issue with the tfa.metrics.F1Score. For your binary case, you will have to change the parameters of the F1Score to tfa.metrics.F1Score(num_classes=1, threshold=0.5). Here is a complete working example:
from tensorflow.keras.layers import Dense, LSTM, Dropout, Input, BatchNormalization
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
import tensorflow_addons as tfa
import tensorflow as tf
def get_model(num_features, output_size, output_bias=0.001):
output_bias = tf.keras.initializers.Constant(output_bias)
opt = Adam(learning_rate=0.0008)
inputs = Input(shape=[None, num_features], dtype=tf.float32, ragged=True)
layers = LSTM(32, activation='tanh')(
inputs.to_tensor(), mask=tf.sequence_mask(inputs.row_lengths()))
layers = BatchNormalization()(layers)
layers = Dropout(0.05)(layers)
layers = Dense(32, activation='relu')(layers)
layers = BatchNormalization()(layers)
layers = Dropout(0.05)(layers)
layers = Dense(32, activation='relu')(layers)
layers = BatchNormalization()(layers)
layers = Dropout(0.05)(layers)
layers = Dense(output_size, activation='sigmoid',
bias_initializer=output_bias)(layers)
model = Model(inputs, layers)
model.compile(loss=tf.keras.losses.BinaryCrossentropy(), optimizer=opt, metrics=[tfa.metrics.F1Score(num_classes=1, threshold=0.5)])
model.summary()
return model
model = get_model(11, 1)
rt = tf.RaggedTensor.from_row_splits(values=tf.ones([5, 11], tf.int32),
row_splits=[0, 2, 5])
model.fit(rt, tf.random.uniform((2,1), maxval=2), epochs=300, batch_size=2, verbose=2)
Alternatively, you just define your own F1Score method and set it as metric in your model. See this post for more information.
dimensionality = 4
#trainint encoder
encoder_inputs = Input(shape=(None, num_encoder_tokens))
decoder_inputs = Input(shape=(None, num_decoder_tokens))
encoder = Bidirectional(LSTM(dimensionality, return_sequences=True, return_state=True,
go_backwards=True), merge_mode='sum')
encoder_outputs, for_h, for_c, bac_h, bac_c = encoder(encoder_inputs)
encoder_states = [tf.add(for_h, for_c), tf.add(bac_h, bac_h) ]
#training decoder
decoder = LSTM(dimensionality, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder(decoder_inputs, initial_state= encoder_states)
dot_prod = dot([decoder_outputs, encoder_outputs], axes=[2, 2])
attention = Activation('softmax', name='attention')
attention_vec = attention(dot_prod)
context = dot([attention_vec, encoder_outputs], axes=[2, 1])
decoder_comb = concatenate([context, decoder_outputs], name='decoder_comb')
dense = Dense(num_decoder_tokens, activation='softmax')
output = dense(decoder_comb)
training_model = Model([encoder_inputs, decoder_inputs], output)
Here you can find summary:
Model: "functional_12"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_13 (InputLayer) [(None, None, 1780)] 0
__________________________________________________________________________________________________
bidirectional_2 (Bidirectional) [(None, None, 4), (N 57120 input_13[0][0]
__________________________________________________________________________________________________
input_14 (InputLayer) [(None, None, 2257)] 0
__________________________________________________________________________________________________
tf_op_layer_Add_4 (TensorFlowOp [(None, 4)] 0 bidirectional_2[0][1]
bidirectional_2[0][2]
__________________________________________________________________________________________________
tf_op_layer_Add_5 (TensorFlowOp [(None, 4)] 0 bidirectional_2[0][3]
bidirectional_2[0][3]
__________________________________________________________________________________________________
lstm_5 (LSTM) [(None, None, 4), (N 36192 input_14[0][0]
tf_op_layer_Add_4[0][0]
tf_op_layer_Add_5[0][0]
__________________________________________________________________________________________________
dot_12 (Dot) (None, None, None) 0 lstm_5[0][0]
bidirectional_2[0][0]
__________________________________________________________________________________________________
attention (Activation) (None, None, None) 0 dot_12[0][0]
__________________________________________________________________________________________________
dot_13 (Dot) (None, None, 4) 0 attention[0][0]
bidirectional_2[0][0]
__________________________________________________________________________________________________
decoder_comb (Concatenate) (None, None, 8) 0 dot_13[0][0]
lstm_5[0][0]
__________________________________________________________________________________________________
dense_2 (Dense) (None, None, 2257) 20313 decoder_comb[0][0]
==================================================================================================
Total params: 113,625
Trainable params: 113,625
Non-trainable params: 0
__________________________________________________________________________________________________
And finally below I paste my attempt to separate encoder and decoder in order to do inference, but it raises an error. I tried to use training_model layers/output/input whenever I could but there is still something that I am missing.
#inference encoder
encoder_model = Model(encoder_inputs, encoder_states)
#inference decoder
decoder_s_h = Input(shape=(dimensionality, ))
decoder_s_c = Input(shape=(dimensionality, ))
decoder_states_inputs = [decoder_s_h, decoder_s_c]
decoder_outputs, state_h, state_c = decoder(decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_h, state_c]
dot_prod = dot([decoder_outputs, encoder_outputs], axes=[2, 2])
attention_vec = attention(dot_prod)
context = dot([attention_vec, encoder_outputs], axes=[2, 1])
decoder_comb = concatenate([context, decoder_outputs])
output= dense(decoder_comb)
decoder_model = Model([decoder_inputs] + decoder_states_inputs, [output] + decoder_states)
I tried so many times to change this configuration but I can't resolve graph disconnection. Could you help me?
PS. I am new to NLP so pls be kind with me, I am a student still not a deep learning specialist...
Thank you so much for your time and help!
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-44-89f9761124cf> in <module>()
18 output= dense(decoder_comb)
19
---> 20 decoder_model = Model([decoder_inputs] + decoder_states_inputs, [output] + decoder_states)
21
22 #encoder decoder model
5 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py in _map_graph_network(inputs, outputs)
929 'The following previous layers '
930 'were accessed without issue: ' +
--> 931 str(layers_with_complete_input))
932 for x in nest.flatten(node.outputs):
933 computable_tensors.add(id(x))
ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_13:0", shape=(None, None, 1780), dtype=float32) at layer "bidirectional_2". The following previous layers were accessed without issue: ['lstm_5']
You can not use input, output properties when creating models with functional api.
Try to change by something like that:
encoder_inputs = Input(shape=(None, num_encoder_tokens))
decoder_inputs = Input(shape=(None, num_decoder_tokens))
encoder_outputs, for_hidden, for_cell, bac_hidden, bac_cell = training_model(encoder_input, decoder_inputs)
Another error relates to this line:
encoder_model = Model(encoder_inputs, encoder_states)
where encoder_states is not dependent from encoder_input. So tensorflow is not able to build graph.
I implemented two models, one time using the sequential way and one time with the functional API. Now the both models give different results, which kind of makes no sense to me.
I cannot figure out, what the problem is. Any ideas or solutions?
Here both models:
Sequential Model:
model = Sequential()
embedding_layer = Embedding(VOCAB_SIZE +1, EMBEDDING_SIZE, mask_zero= True)
model.add(embedding_layer)
model.add(Bidirectional(LSTM(HIDDEN_SIZE, return_sequences= True))),
model.add(TimeDistributed(Dense(NUM_LABELS, activation='softmax')))
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_sents_padded, train_labels_padded, batch_size=4, epochs=10,
validation_data=(dev_sents_padded, dev_labels_padded))
score, acc = model.evaluate(dev_sents_padded, dev_labels_padded)
print("\nAccuracy: ", acc)
Functional Model:
inputs = Input(shape=(MAX_LENGTH,))
embedding = Embedding(VOCAB_SIZE +1, EMBEDDING_SIZE, mask_zero= True)(inputs)
left = LSTM(HIDDEN_SIZE, return_sequences=True)(embedding)
right = LSTM(HIDDEN_SIZE, go_backwards=True, return_sequences=True)
(embedding)
left_right = concatenate([left, right])
left_right = TimeDistributed(Dense(NUM_LABELS, activation='softmax'))
(left_right)
combined_model = Model(inputs=inputs, outputs=left_right)
combined_model.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
combined_model.fit(
train_sents_padded,
train_labels_padded,
batch_size=4,
epochs=10,
validation_data=(dev_sents_padded, dev_labels_padded)
)
score, acc = combined_model.evaluate(dev_sents_padded, dev_labels_padded)
print("\nBidirectional LSTM Accuracy: ", acc)
+++
Summaries:
Sequential model:
Layer (type) Output Shape Param #
=================================================================
embedding_1 (Embedding) (None, None, 50) 26150
_________________________________________________________________
bidirectional_1 (Bidirection (None, None, 100) 40400
_________________________________________________________________
time_distributed_1 (TimeDist (None, None, 61) 6161
=================================================================
Total params: 72,711
Trainable params: 72,711
Non-trainable params: 0 `
Functional model:
Layer (type) Output Shape Param # Connected to
========================================================================
input_1 (InputLayer) (None, 34) 0
____________________________________________________________________
embedding_2 (Embedding) (None, 34, 50) 26150 input_1[0][0]
______________________________________________________________________
lstm_2 (LSTM) (None, 34, 50) 20200 embedding_2[0][0]
____________________________________________________________________
lstm_3 (LSTM) (None, 34, 50) 20200 embedding_2[0][0]
_________________________________________________________________
concatenate_1 (Concatenate)(None, 34, 100) 0 lstm_2[0][0]
lstm_3[0][0]
___________________________________________________________
time_distributed_2 (TimeDistrib (None, 34, 61) 6161 concatenate_1[0][0]
=====================================================================
Total params: 72,711
Trainable params: 72,711
Non-trainable params: 0`
+++
If I change VOCAB_SIZE + 1 to VOCAB_SIZE in the seqential model the acc is 59, but only on every third run??