Unable to add two layers in Keras of Tensorflow - python

I have a simple regression model as below. The layers layer_abc and layer_efg both have (None, 5) as output, so their output have same dimension and can be added. Thus I want to unhide the code #keras.layers.Add()(['layer_abc', 'layer_efg']). But whenever I do this, I got an error AttributeError: 'str' object has no attribute 'get_shape'. If I didn't unhide this line, then the code is fine.
How can I add the two layers without having error? Many thanks!
from __future__ import absolute_import, division, print_function
from scipy import misc
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization, Activation
import numpy as np
import matplotlib.pyplot as plt
train_images=np.array([[[0],[1],[2]],[[0],[0],[2]],[[1],[1],[1]],[[1],[0],[1]]])
train_labels=np.array([[1],[0],[1],[0]])
model = keras.Sequential([
keras.layers.Flatten(input_shape=(3, 1)),
keras.layers.Dense(5, activation=tf.nn.relu,name='layer_abc'),
keras.layers.Dense(5, activation=tf.nn.relu,name='layer_efg'),
#keras.layers.Add()(['layer_abc', 'layer_efg']),
keras.layers.Dense(1, activation=tf.nn.softmax),
])
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['accuracy','mean_squared_error'])
print(model.summary())
model.fit(train_images, train_labels, epochs=2)

You can use the functional API like this to do the Add, for single output between 0 and 1, use the sigmoid activation for the output:
input = keras.layers.Input((3,1))
x1 = keras.layers.Dense(5, activation=tf.nn.relu, name='layer_abc')(input)
x2 = keras.layers.Dense(5, activation=tf.nn.relu, name='layer_efg')(input)
x = keras.layers.Add()([x1, x2])
x = keras.layers.Flatten()(x)
output = keras.layers.Dense(1, activation=tf.nn.sigmoid)(x)
model = keras.models.Model(input, output)
This might work:
model = keras.Sequential([
keras.layers.Flatten(input_shape=(3, 1)),
keras.layers.Dense(5, activation=tf.nn.relu,name='layer_abc'),
keras.layers.Dense(5, activation=tf.nn.relu,name='layer_efg')])
model.add(keras.layers.Lambda(lambda x: tf.add(model.layers[1].output, x)))
model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid))

Related

Intermediate Output of let' s say Resnet50 from Keras Model

import keras
print(keras.__version__)
#2.3.0
from keras.models import Sequential
from keras.layers import Input, Dense,TimeDistributed
from keras.models import Model
model = Sequential()
resnet = ResNet50(include_top = False, pooling = 'avg', weights = 'imagenet')
model.add(resnet)
model.add(Dense(10, activation = 'relu'))
model.add(Dense(6, activation = 'sigmoid'))
model.summary()
// Training // model.fit( .. ) done
now how to just the output from layer ?
model.layers[0]._name='resnet50'
print(model.layers[0].name) # prints resnet50
layer_output = model.get_layer("resnet50").output
intermediate_model = Model(inputs=[model.input, resnet.input], outputs=[layer_output])
result = intermediate_model.predict([x, x])
print(result.shape)
print(result[0].shape)
Got Error
AttributeError: Layer resnet50 has multiple inbound nodes, hence the
notion of "layer output" is ill-defined. Use
get_output_at(node_index) instead. add Codeadd Markdown
Please try again using tf.keras to import model and layers.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, Dense,TimeDistributed
from tensorflow.keras.models import Model
and then run the same:
model.layers[0]._name='resnet50'
print(model.layers[0].name) # prints resnet50
layer_output = model.get_layer("resnet50").output
intermediate_model = Model(inputs=[model.input, resnet.input], outputs=[layer_output])
x = tf.ones((1, 250, 250, 3))
result = intermediate_model.predict([x, x])
print(result.shape)
print(result[0].shape)
Output:
resnet50
(1, 2048)
(2048,)

A target array with shape (11203, 25) was passed for an output of shape (None, 3) while using as loss `categorical_crossentropy`

I am a beginner in text processing techniques and I am trying to execute the below code.
from keras.layers import Dense, Input, GlobalMaxPooling1D
from keras.layers import Conv1D, MaxPooling1D, Embedding
from keras.models import Model
from keras.layers import Input, Dense, Embedding, Conv2D, MaxPooling2D, Dropout,concatenate
from keras.layers.core import Reshape, Flatten
from keras.callbacks import EarlyStopping
from keras.optimizers import Adam
from keras.models import Model
from keras import regularizers
sequence_length = trn_abs.shape[1]
filter_sizes = [3,4,5]
num_filters = 100
drop = 0.5
inputs = Input(shape=(sequence_length,))
embedding = embedding_layer(inputs)
reshape = Reshape((sequence_length,embedding_dim,1))(embedding)
conv_0 = Conv2D(num_filters, (filter_sizes[0], embedding_dim),activation='relu',kernel_regularizer=regularizers.l2(0.01))(reshape)
conv_1 = Conv2D(num_filters, (filter_sizes[1], embedding_dim),activation='relu',kernel_regularizer=regularizers.l2(0.01))(reshape)
conv_2 = Conv2D(num_filters, (filter_sizes[2], embedding_dim),activation='relu',kernel_regularizer=regularizers.l2(0.01))(reshape)
maxpool_0 = MaxPooling2D((sequence_length - filter_sizes[0] + 1, 1), strides=(1,1))(conv_0)
maxpool_1 = MaxPooling2D((sequence_length - filter_sizes[1] + 1, 1), strides=(1,1))(conv_1)
maxpool_2 = MaxPooling2D((sequence_length - filter_sizes[2] + 1, 1), strides=(1,1))(conv_2)
merged_tensor = concatenate([maxpool_0, maxpool_1, maxpool_2], axis=1)
flatten = Flatten()(merged_tensor)
reshape = Reshape((3*num_filters,))(flatten)
dropout = Dropout(drop)(flatten)
output = Dense(units=3, activation='softmax',kernel_regularizer=regularizers.l2(0.01))(dropout)
# this creates a model that includes
model = Model(inputs, output)
adam = Adam(lr=1e-3)
model.compile(loss='categorical_crossentropy',
optimizer=adam,
metrics=['acc'])
callbacks = [EarlyStopping(monitor='val_loss')]
model.fit(X_trn, trn[target_cols], epochs=100)
and I am getting the following error:
ValueError: A target array with shape (11203, 25) was passed for output of shape (None, 3) while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output.
Could anyone help me with this, I am new to stackoverflow too,so please accept my apologies for ill-formating of question.
It's really important that the number of neurons at the end of your neural network is the number of categories you have. So try this:
output = Dense(units=25, activation='softmax'...

error in grid search optimization -ValueError: Could not interpret optimizer identifier: <keras.optimizers.Adam object at 0x0000027A2BE4BAC8>

i tried to optimize my DL model using the grid search algorithm
it runs and when it complete it arise the following error - note that i used both tensorflow and keras - cause i didn't understand the reason of the error.
i try to import just each of them , but i doesnt work also
import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import InputLayer
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from keras.layers import Dropout
from tensorflow.keras import regularizers
from tensorflow.keras.models import load_model
from tensorflow.keras.layers import concatenate
from tensorflow.keras.models import Model
from numpy import argmax
from sklearn.model_selection import GridSearchCV, KFold
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.wrappers.scikit_learn import KerasClassifier
from tensorflow.keras import optimizers
from keras.optimizers import Adam
from keras.models import Sequential
from keras.layers import Dense,Dropout
#Grid search optimization
def create_model(learn_rate, dropout_rate):
# Create model
model = keras.Sequential()
model.add(layers.Dense(260, input_dim=265, activation='relu'))
model.add(layers.Dense(240, activation='relu',kernel_regularizer=regularizers.l2(0.01)))
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dropout(0.1))
model.add(layers.Dense(56, activation='relu'))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dropout(0.1))
model.add( layers.Dense(1, activation='sigmoid', name='class'))
# Compile the model
adam = Adam(lr=learn_rate)
model.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])
return model
# Create the model
model = KerasClassifier(build_fn=create_model, verbose=1)
learn_rate = [0.001, 0.02, 0.2]
dropout_rate = [0.0, 0.2, 0.4]
batch_size = [10, 20, 30]
epochs = [1, 5, 10]
seed = 42
# Make a dictionary of the grid search parameters
param_grid = dict(learn_rate=learn_rate, dropout_rate=dropout_rate, batch_size=batch_size, epochs=epochs )
# Build and fit the GridSearchCV
grid = GridSearchCV(estimator=model, param_grid=param_grid,
cv=KFold(random_state=seed), verbose=10)
grid_results = grid.fit(X_train, Y_train)
when i run grid.fit
it runs then show the following error
ValueError: Could not interpret optimizer identifier: <keras.optimizers.Adam object at 0x0000027A2BE4BAC8>
any help will be appreciated

ValueError: Graph disconnected: cannot obtain value for tensor Tensor...The following previous layers were accessed without issue: []

I have been trying to create a multi-input model using Keras, but got errors. The idea is to combine the text and corresonding topics to make predictions for sentiments. Here's the code:
import numpy as np
text = np.random.randint(5000, size=(442702, 200), dtype='int32')
topic = np.random.randint(2, size=(442702, 227), dtype='int32')
sentiment = to_categorical(np.random.randint(5, size=442702), dtype='int32')
from keras.models import Sequential
from keras.layers import Dense, Activation, Embedding, Flatten, GlobalMaxPool1D, Dropout, Conv1D
from keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint
from keras.losses import binary_crossentropy
from keras.optimizers import Adam
text_input = Input(shape=(200,), dtype='int32', name='text')
text_encoded = Embedding(input_dim=5000, output_dim=20, input_length=200)(text_input)
text_encoded = Dropout(0.1)(text_encoded)
text_encoded = Conv1D(300, 3, padding='valid', activation='relu', strides=1)(text_encoded)
text_encoded = GlobalMaxPool1D()(text_encoded)
topic_input = Input(shape=(227,), dtype='int32', name='topic')
concatenated = concatenate([text_encoded, topic_input])
sentiment = Dense(5, activation='softmax')(concatenated)
model = Model(inputs=[text_encoded, topic_input], outputs=sentiment)
# summarize layers
print(model.summary())
# plot graph
plot_model(model)
However, this gives me the below error:
TypeError: Tensors in list passed to 'values' of 'ConcatV2' Op have types [float32, int32] that don't all match.
Now if I change dtype of topic_input from 'int32' to 'float32', I got a different error:
ValueError: Graph disconnected: cannot obtain value for tensor Tensor("text_37:0", shape=(?, 200), dtype=int32) at layer "text". The following previous layers were accessed without issue: []
On the other hand, part of the model works just fine with the sequential API.
model = Sequential()
model.add(Embedding(5000, 20, input_length=200))
model.add(Dropout(0.1))
model.add(Conv1D(300, 3, padding='valid', activation='relu', strides=1))
model.add(GlobalMaxPool1D())
model.add(Dense(227))
model.add(Activation('sigmoid'))
print(model.summary())
Any pointers are highly appreciated.
There are few issues with your Keras functional API implementation,
You should use the Concatenate layer as Concatenate(axis=-1)([text_encoded, topic_input]).
In the concatenate layer you are trying to combine an int32 tensor and a float32 tensor, which is not allowed. What you should do is, from keras.backend import cast and concatenated = Concatenate(axis=-1)([text_encoded, cast(topic_input, 'float32')]).
You got variable conflicts, there are two sentiment variables, one pointing to a to_categorical output and the other the output of the final Dense layer.
Your model inputs cannot be intermediate tensors like text_encoded. They should come from Input layers.
To help with your implementation, here's a working version of your code (I am not sure if this is exactly what you wanted though) in TF 1.13.
from keras.utils import to_categorical
text = np.random.randint(5000, size=(442702, 200), dtype='int32')
topic = np.random.randint(2, size=(442702, 227), dtype='int32')
sentiment1 = to_categorical(np.random.randint(5, size=442702), dtype='int32')
from keras.models import Sequential
from keras.layers import Input, Dense, Activation, Embedding, Flatten, GlobalMaxPool1D, Dropout, Conv1D, Concatenate, Lambda
from keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint
from keras.losses import binary_crossentropy
from keras.optimizers import Adam
from keras.backend import cast
from keras.models import Model
text_input = Input(shape=(200,), dtype='int32', name='text')
text_encoded = Embedding(input_dim=5000, output_dim=20, input_length=200)(text_input)
text_encoded = Dropout(0.1)(text_encoded)
text_encoded = Conv1D(300, 3, padding='valid', activation='relu', strides=1)(text_encoded)
text_encoded = GlobalMaxPool1D()(text_encoded)
topic_input = Input(shape=(227,), dtype='int32', name='topic')
topic_float = Lambda(lambda x:cast(x, 'float32'), name='Floatconverter')(topic_input)
concatenated = Concatenate(axis=-1)([text_encoded, topic_float])
sentiment = Dense(5, activation='softmax')(concatenated)
model = Model(inputs=[text_input, topic_input], outputs=sentiment)
# summarize layers
print(model.summary())
Hope these help.

'tf' is not defined on load_model() - using lambda

I have a Keras model that I am trying to export and use in a different python code.
Here is my code:
from keras.models import Sequential
from keras.layers import Dense, Embedding, LSTM, GRU, Flatten, Dropout, Lambda
from keras.layers.embeddings import Embedding
import tensorflow as tf
EMBEDDING_DIM = 100
model = Sequential()
model.add(Embedding(vocab_size, 300, weights=[embedding_matrix], input_length=max_length, trainable=False))
model.add(Lambda(lambda x: tf.reduce_mean(x, axis=1)))
model.add(Dense(8, input_dim=4, activation='relu'))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train_pad, y_train, batch_size=128, epochs=25, validation_data=(X_val_pad, y_val), verbose=2)
model.save('my_model.h5')
In another file, when I import my_model.h5 :
from keras.models import load_model
from keras.layers import Lambda
import tensorflow as tf
def learning(test_samples):
model = load_model('my_model.h5')
#ERROR HERE
#rest of the code
The error is the following:
in <lambda>
model.add(Lambda(lambda x: tf.reduce_mean(x, axis=1)))
NameError: name 'tf' is not defined
After research, I got that the fact that I used lambda in my model is the reason for this problem, but I added these references and it didn't help:
from keras.models import load_model
from keras.layers import Lambda
import tensorflow as tf
What could be the problem?
Thank you
When loading the model, you need to explicitly handle custom objects or custom layers (CTRL+f the docs for Handling custom layers):
import tensorflow as tf
import keras
model = keras.models.load_model('my_model.h5', custom_objects={'tf': tf})
It happened to me too. You need to import tensorflow inside your lambda function. So you probably want to put the code in a separate function:
def reduce_mean(x):
import tensorflow as tf
return tf.reduce_mean(x, axis=1)
model = Sequential()
model.add(Embedding(vocab_size, 300, weights=[embedding_matrix], input_length=max_length, trainable=False))
model.add(Lambda(reduce_mean))
model.add(Dense(8, input_dim=4, activation='relu'))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train_pad, y_train, batch_size=128, epochs=25, validation_data=(X_val_pad, y_val), verbose=2)
model.save('my_model.h5')
Besides, when you try to load the model structure from a json file, passing custom objects solves the problem.
model = model_from_json(open("model_structure.json", "r").read(), custom_objects={'tf': tf})
One weird solution that has worked for me is to import the module used to create the model. So say you have file you use to create a model and save it.
make_model.py
from keras.models import Sequential
from keras.layers import Dense, Embedding, LSTM, GRU, Flatten, Dropout, Lambda
from keras.layers.embeddings import Embedding
import tensorflow as tf
EMBEDDING_DIM = 100
model = Sequential()
model.add(Embedding(vocab_size, 300, weights=[embedding_matrix], input_length=max_length, trainable=False))
model.add(Lambda(lambda x: tf.reduce_mean(x, axis=1)))
...
model.fit(X_train_pad, y_train, batch_size=128, epochs=25, validation_data=(X_val_pad, y_val), verbose=2)
model.save('my_model.h5')
If you load the model in another file load_model.py, you may be able to get around the error via import of the first module.
from keras.models import load_model
from keras.layers import Lambda
import tensorflow as tf
import make_model
def learning(test_samples):
model = load_model('my_model.h5')
This worked for me since the error read
UserWarning: make_model is not loaded, but a Lambda layer uses it. It may cause errors.
...
NameError: name 'tf' is not defined
Happened to me as well - however problem was that the code was refactored and the Lambda layer was replaced by something else. In this case, you can always rollback to the point where it worked.
The simple workaround in case you are not able to restore the previous solution is adding: custom_objects={'tf': tf} to restore_model call

Categories