I want to create VAE(variational autoencoder). During model creating it throws exception.
When subclassing the Model class, you should implement a call method.
I am using Tensorflow 2.0
def vae():
models ={}
def apply_bn_and_dropout(x):
return l.Dropout(dropout_rate)(l.BatchNormalization()(x))
input_image = l.Input(batch_shape=(batch_size,28,28,1))
x = l.Flatten()(input_image)
x = l.Dense(256,activation="relu")(x)
x = apply_bn_and_dropout(x)
x = l.Dense(128,activation="relu")(x)
x = apply_bn_and_dropout(x)
z_mean = l.Dense(latent_dim)(x)
z_log_var = l.Dense(latent_dim)(x)
def sampling(args):
z_mean, z_log_var = args
epsilon = K.random_normal(shape=(batch_size,latent_dim),mean=0., stddev=1.0)
return z_mean + K.exp(z_log_var/2) * epsilon
lambda_layer = l.Lambda(sampling,output_shape=(latent_dim,))([z_mean,z_log_var])
models["encoder"] = Model(input_image,lambda_layer,"Encoder")
models["z_meaner"] = Model(input_image,z_mean,"Enc_z_mean")
models["z_lvarer"] = Model(input_image, z_log_var,"Enc_z_log_var")
z = l.Input(shape=(latent_dim,))
x = l.Dense(128)(z)
x = l.LeakyReLU()(x)
x = apply_bn_and_dropout(x)
x = l.Dense(256)(x)
x = l.LeakyReLU()(x)
x = apply_bn_and_dropout(x)
x = l.Dense(28*28,activation="sigmoid")(x)
decoded = l.Reshape((28,28,1))(x)
models["decoder"] = Model(z,decoded,name="Decoder")
models["vae"] = Model(input_image, models["decoder"](models["encoder"](input_image)), name="VAE")
def vae_loss(x,decoded):
x = K.reshape(x,shape=(batch_size,28*28))
decoded = K.reshape(decoded,shape=(batch_size,28*28))
xent_loss = 28*28*binary_crossentropy(x, decoded)
kl_loss = -0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
return (xent_loss + kl_loss)/2/28/28
return models, vae_loss
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-34-186b31069dc3> in <module>
----> 1 models, vae_loss = vae()
2 vae = models["vae"]
<ipython-input-33-0fa06b39e41c> in vae()
36
37 models["decoder"] = Model(z,decoded,name="Decoder")
---> 38 models["vae"] = Model(input_image, models["decoder"](models["encoder"](input_image)), name="VAE")
39
40 def vae_loss(x,decoded):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
610 base_layer_utils.AutoAddUpdates(self,
611 inputs)) as auto_updater:
--> 612 outputs = self.call(inputs, *args, **kwargs)
613 auto_updater.set_outputs(outputs)
614
~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\keras\engine\network.py in call(self, inputs, training, mask)
865 """
866 if not self._is_graph_network:
--> 867 raise NotImplementedError('When subclassing the `Model` class, you should'
868 ' implement a `call` method.')
869
NotImplementedError: When subclassing the `Model` class, you should implement a `call` method.
Models with names
def create_dense_ae():
encoding_dim = 64
input_img = layers.Input(shape=(28, 28, 1))
flat_img = layers.Flatten()(input_img)
encoded = layers.Dense(encoding_dim, activation='relu')(flat_img)
input_encoded = layers.Input(shape=(encoding_dim,))
flat_decoded = layers.Dense(28*28, activation='sigmoid')(input_encoded)
decoded = layers.Reshape((28, 28, 1))(flat_decoded)
encoder = tf.keras.Model(input_img, encoded, name="encoder")
decoder = tf.keras.Model(input_encoded, decoded, name="decoder")
autoencoder = tf.keras.Model(input_img, decoder(encoder(input_img)), name="autoencoder")
return encoder, decoder, autoencoder
I want to get model.
The problem is here:
models["encoder"] = Model(input_image,lambda_layer,"Encoder")
models["z_meaner"] = Model(input_image,z_mean,"Enc_z_mean")
models["z_lvarer"] = Model(input_image, z_log_var,"Enc_z_log_var")
You are passing three arguments to the construction, where only two are needed (inputs and outputs). Models do not have names. The problem is that three parameters will break the detection of network or sub-classed model as shown in the keras source code.
So just replace the code with:
models["encoder"] = Model(input_image,lambda_layer)
models["z_meaner"] = Model(input_image,z_mean)
models["z_lvarer"] = Model(input_image, z_log_var)
Related
I just need a little help regarding my project. I've already written a code but I am facing an error in that. I am using a few-shot learning technique triplet neural network. The triplet neural network(TNN) is a horizontal concatenation triplet consisting of three identical Convolutional Neural Networks (with common parameters) that are trained with triplets of inputs. An anchor instance, a positive instance (of the same class as the anchor), and a negative instance make up the input triplet (different class from the anchor). After that, the network is trained to learn a triplet loss embedding function. To compute triplet loss, three training examples are required. Each triplet is formed by intentionally selecting training examples such that each triplet has:
• a reference image called anchor image
• an image having the same label as anchor is called a positive image
• an image has a different label than the anchor called a negative image.
The TNN learns to create k-dimensional feature vector representation of images in such a manner that similar images lie closer in an embedding space of k-dimensions.
embedding_dimension = 128
from tensorflow.keras.applications.vgg16 import VGG16
pre_trained_vgg16 = tf.keras.applications.VGG16(
input_shape=(size, size,3),
include_top=False,
weights="imagenet",
input_tensor=None,
pooling=None,
classes=1000,
classifier_activation=None
)
pre_trained_vgg16.save('vgg16.h5')
pre_trained_vgg16 = tf.keras.models.load_model('vgg16.h5')
def build_embedding_network(embedding_dimension):
embedding_network = pre_trained_vgg16
embedding_network.trainable = False
x = embedding_network.output
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dropout(0.2)(x)
x = tf.keras.layers.Dense(2*embedding_dimension, activation='sigmoid')(x)
x = tf.keras.layers.Dense(embedding_dimension, activation='sigmoid')(x)
embedding_network = tf.keras.Model(embedding_network.input, x, name="embedding_network")
return embedding_network
def build_metric_network(single_embedding_dim):
input1 = tf.keras.layers.Input((single_embedding_dim), name="input1")
input2 = tf.keras.layers.Input((single_embedding_dim), name="input2")
embedded_distance = tf.keras.layers.Subtract(name='subtract_embeddings')([input1, input2])
embedded_distance = tf.keras.layers.Lambda(lambda x: K.sqrt(K.sum(K.square(x), axis=-1, keepdims=True)),
name='euclidean_distance')(embedded_distance)
metric_network = tf.keras.Model(inputs=[input1, input2],
outputs=[embedded_distance],
name="metric_network")
return metric_network
class TripletLossLayer(tf.keras.layers.Layer):
def __init__(self, margin, **kwargs):
self.margin = margin
super(TripletLossLayer, self).__init__(**kwargs)
def triplet_loss(self, inputs):
ap_dist, an_dist = inputs
#square
ap_dist2 = K.square(ap_dist)
an_dist2 = K.square(an_dist)
return K.sum(K.maximum(ap_dist2 - an_dist2 + self.margin, 0))
def call(self, inputs):
loss = self.triplet_loss(inputs)
self.add_loss(loss)
return loss
def get_config(self):
config = super().get_config().copy()
config.update({
'margin': self.margin
})
return config
def build_triplet_snn(input_shape, embedding_network, metric_network, margin=0.1):
# Define the tensors for the three input images
anchor_input = tf.keras.layers.Input(input_shape, name="anchor_input")
positive_input = tf.keras.layers.Input(input_shape, name="positive_input")
negative_input = tf.keras.layers.Input(input_shape, name="negative_input")
# Generate the embeddings (feature vectors) for the three images
embedding_a = embedding_network(anchor_input)
embedding_p = embedding_network(positive_input)
embedding_n = embedding_network(negative_input)
ap_dist = metric_network([embedding_a,embedding_p])
an_dist = metric_network([embedding_a,embedding_n])
# Triplet loss layer
loss_layer = TripletLossLayer(margin=margin, name='TripletLossLayer')([ap_dist, an_dist])
# Compute the concatenated pairs
all_concatenated = tf.keras.layers.Concatenate(axis=-1,name="All-Embeddings")([embedding_a,embedding_p,embedding_n])
# Connect the inputs with the outputs
triplet_snn = tf.keras.Model(inputs=[anchor_input, positive_input, negative_input],
outputs=[loss_layer, all_concatenated],
name="triplet_snn")
# Return the model
return triplet_snn
embedding_network = build_embedding_network(embedding_dimension)
metric_network = build_metric_network(embedding_dimension)
triplet_snn = build_triplet_snn(
input_shape=(size, size,3),
embedding_network=embedding_network,
metric_network=metric_network,
margin=0.1
)
learning_rate = 0.0001
epochs = 5
class TripletDataGenerator(tf.keras.utils.Sequence):
def __init__(self, triplet_dataset, shuffle=False):
self.triplet_dataset = triplet_dataset
self.shuffle = shuffle
self.on_epoch_end()
def __len__(self):
return len(self.triplet_dataset)
def __getitem__(self, index):
return triplet_dataset[index][0]
#return (np.array(triplet_dataset[index][0]).reshape(1,224,224,3))
def on_epoch_end(self):
if self.shuffle == True:
random.shuffle(self.triplet_dataset)
data_gen = TripletDataGenerator(triplet_dataset)
filepath = 'C:\\Users\\Y540\\Desktop\\Retinal Disease\\TrainedSNN\\temp\\weights.{epoch}'
save_model_weights_at_every_epoch = tf.keras.callbacks.ModelCheckpoint(
filepath,
monitor="loss",
verbose=1,
save_best_only=False,
save_weights_only=True,
mode="auto",
save_freq="epoch"
)
optimizer = tf.keras.optimizers.Adam(lr=learning_rate)
triplet_snn.compile(loss=None, optimizer=optimizer, run_eagerly=True)
%%time
history = triplet_snn.fit(data_gen, epochs=epochs, verbose=1, callbacks=[save_model_weights_at_every_epoch])
The error I am getting is:
this is the error that I am getting
Epoch 1/5
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<timed exec> in <module>
~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
~\anaconda3\lib\site-packages\keras\engine\input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
198
199 if len(inputs) != len(input_spec):
--> 200 raise ValueError(f'Layer "{layer_name}" expects {len(input_spec)} input(s),'
201 f' but it received {len(inputs)} input tensors. '
202 f'Inputs received: {inputs}')
ValueError: Layer "triplet_snn" expects 3 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor: shape=(1, 224, 224, 3), dtype=float32, numpy=
I am working on clinical EHR. I am currently referring to this blog and github link here. I have generated the dataset and processed it as per the instructions in the notebooks present in the repository. I am facing an issue trying to train the model.
build_EHRNN class.
torch.manual_seed(1)
class build_EHRNN(nn.Module):
def __init__(self, inputDimSize=4894, hiddenDimSize=[200,200], batchSize=100, embSize=200, numClass=4894, dropout=0.5, logEps=1e-8):
super(build_EHRNN, self).__init__()
self.inputDimSize = inputDimSize
self.hiddenDimSize = hiddenDimSize
self.numClass = numClass
self.embSize = embSize
self.batchSize = batchSize
self.dropout = nn.Dropout(p=0.5)
self.logEps = logEps
# Embedding inputs
self.W_emb = nn.Parameter(torch.randn(self.inputDimSize, self.embSize).cuda())
self.b_emb = nn.Parameter(torch.zeros(self.embSize).cuda())
self.W_out = nn.Parameter(torch.randn(self.hiddenDimSize, self.numClass).cuda())
self.b_out = nn.Parameter(torch.zeros(self.numClass).cuda())
self.params = [self.W_emb, self.W_out,
self.b_emb, self.b_out]
def forward(self,x, y, h, lengths, mask):
self.emb = torch.tanh(torch.matmul(x, self.W_emb) + self.b_emb)
input_values = self.emb
self.outputs = [input_values]
for i, hiddenSize in enumerate([self.hiddenDimSize, self.hiddenDimSize]): # iterate over layers
rnn = EHRNN(self.inputDimSize,hiddenSize,self.embSize,self.batchSize,self.numClass) # calculate hidden states
hidden_state = []
h = self.init_hidden().cuda()
for i,seq in enumerate(input_values): # loop over sequences in each batch
h = rnn(seq, h)
hidden_state.append(h)
hidden_state = self.dropout(torch.stack(hidden_state)) # apply dropout between layers
input_values = hidden_state
y_linear = torch.matmul(hidden_state, self.W_out) + self.b_out # fully connected layer
yhat = F.softmax(y_linear, dim=1) # yhat
yhat = yhat*mask[:,:,None] # apply mask
# Loss calculation
cross_entropy = -(y * torch.log(yhat + self.logEps) + (1. - y) * torch.log(1. - yhat + self.logEps))
last_step = -torch.mean(y[-1] * torch.log(yhat[-1] + self.logEps) + (1. - y[-1]) * torch.log(1. - yhat[-1] + self.logEps))
prediction_loss = torch.sum(torch.sum(cross_entropy, dim=0),dim=1)/ torch.cuda.FloatTensor(lengths)
cost = torch.mean(prediction_loss) + 0.000001 * (self.W_out ** 2).sum() # regularize
return (yhat, hidden_state, cost)
def init_hidden(self):
return torch.zeros(self.batchSize, self.hiddenDimSize) # initial state
Creating instance and training model
model = build_EHRNN(inputDimSize=4894, hiddenDimSize=200, batchSize=100, embSize=200, numClass=4894, dropout=0.5, logEps=1e-8)
model = model.to(device)
optimizer = torch.optim.Adadelta(model.parameters(), lr = 0.01, rho=0.90)
max_epochs = 10
loss_all = []
iteration = 0
for e in range(max_epochs):
for index in random.sample(range(n_batches), n_batches):
batchX = train[0][:n_batches*batchSize][index*batchSize:(index+1)*batchSize]
batchY = train[1][:n_batches*batchSize][index*batchSize:(index+1)*batchSize]
optimizer.zero_grad()
x, y, lengths, mask = padding(batchX, batchY, 4894, 4894)
if torch.cuda.is_available():
x, y, lenghts, mask = x.cuda(), y.cuda(), lengths, mask.cuda()
outputs, hidden, cost = model(x,y, h, lengths, mask)
if torch.cuda.is_available():
cost.cuda()
cost.backward()
nn.utils.clip_grad_norm_(model.parameters(), 5)
optimizer.step()
Error:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-14-cff1f002dced> in <module>()
17 x, y, lenghts, mask = x.cuda(), y.cuda(), lengths, mask.cuda()
18
---> 19 outputs, hidden, cost = model(x,y, h, lengths, mask)
20
21 if torch.cuda.is_available():
NameError: name 'h' is not defined
Update:
Removing 'h' param produces the following error
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-14-6495250d91c9> in <module>()
18
19 # outputs, hidden, cost = model(x,y, h, lengths, mask)
---> 20 outputs, hidden, cost = model(x, y, lengths, mask)
21
22 if torch.cuda.is_available():
1 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
<ipython-input-7-3c831fe3ca8d> in forward(self, x, y, lengths, mask)
36 h = rnn(seq, h)
37 hidden_state.append(h)
---> 38 hidden_state = self.dropout(torch.stack(hidden_state)) # apply dropout between layers
39 input_values = hidden_state
40
RuntimeError: stack expects a non-empty TensorList
I think I fixed your error:
replace your forward method to:
def forward(self,x, y, lengths, mask):
self.emb = torch.tanh(torch.matmul(x, self.W_emb) + self.b_emb)
input_values = self.emb
self.outputs = [input_values]
for i, hiddenSize in enumerate([self.hiddenDimSize, self.hiddenDimSize]): # iterate over layers
rnn = EHRNN(self.inputDimSize,hiddenSize,self.embSize,self.batchSize,self.numClass) # calculate hidden states
hidden_state = []
h = self.init_hidden().cuda()
for i,seq in enumerate(input_values): # loop over sequences in each batch
h = rnn(seq, h)
hidden_state.append(h)
hidden_state = self.dropout(torch.stack(hidden_state)) # apply dropout between layers
input_values = hidden_state
y_linear = torch.matmul(hidden_state, self.W_out) + self.b_out # fully connected layer
yhat = F.softmax(y_linear, dim=1) # yhat
yhat = yhat*mask[:,:,None] # apply mask
and replace the line where the error happens to:
outputs, hidden, cost = model(x, y, lengths, mask)
I'm having problems using functional api for estimating by maximizing
First I minimize the error vector by maximizing the probability layer loss, and then I want to use the mean vector layer to rank xc_hat similar embeddings.
The code is as follows:
import random as rdn
import tensorflow.compat.v2 as tf
tf.enable_v2_behavior()
import tensorflow_probability as tfp
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
tfd = tfp.distributions
n_observations = 2000
n_features = 5
d_dim = 3
lr = 0.005
# Generate toy data
def make_relations(x_tr, y_tr, c_tr):
# Generate input data centers being labels (xc) for two random
# in-cluster (xa, xb)
xa = []
xc = []
xb = []
for l in y_tr:
kone = [k for k, lab in zip(x_tr, y_tr) if lab==l]
if len(kone) < 3:
continue
for i, x in enumerate(kone):
if np.isclose(x, c_tr[l]).all():
continue
kone_minus_x = kone.copy()
kone_minus_x.pop(i)
print
xa.append(x)
xc.append(c_tr[l])
xb.append(rdn.choice(kone_minus_x))
return np.vstack(xa), np.vstack(xb), np.vstack(xc)
X, Y, C = make_blobs(n_samples=n_observations,
n_features=n_features,
centers=int(n_observations*0.2),
return_centers=True)
x_a, x_b, x_c = make_relations(X, Y, C)
Xa_train, Xa_test = train_test_split(x_a, test_size=.4)
Xb_train, Xb_test = train_test_split(x_b, test_size=.4)
Xc_train, Xc_test = train_test_split(x_c, test_size=.4)
Xa_train = Xa_train[np.newaxis]
Xb_train = Xb_train[np.newaxis]
Xc_train = Xc_train[np.newaxis]
Xa_test = Xa_test[np.newaxis]
Xb_test = Xb_test[np.newaxis]
Xc_test = Xc_test[np.newaxis]
neg_log_likelihood = lambda y, rv_y: -rv_y.log_prob(y)
ones_train = tf.keras.backend.ones((1, Xc_train.shape[1], d_dim)).numpy()
ones_test = tf.keras.backend.ones((1, Xc_test.shape[1], d_dim)).numpy()
# Build model.
xa_xb = tf.keras.layers.Input(shape=(None, n_features), name='Xa-Xb')
L_xa_xb = tf.keras.layers.Dense(d_dim, activation='sigmoid', name='L_Xa-Xb')(xa_xb)
xb = tf.keras.layers.Input(shape=(None, n_features), name='Xb')
L_xb = tf.keras.layers.Dense(d_dim, activation='sigmoid', name='L_Xb')(xb)
mu = tf.keras.layers.Add(name='mean_vector')([L_xa_xb, L_xb])
xc = tf.keras.layers.Input(shape=(None, n_features), name='Xc')
L_xc = tf.keras.layers.Dense(d_dim, name='L_Xc')(xc)
error_vector = tf.keras.layers.Subtract(name='error_vector')([L_xc, mu])
p_xc_given_xa_xb = tfp.layers.DistributionLambda(
lambda t: tfd.Normal(loc=t, scale=tf.exp(t)), name='Gaussian')(error_vector)
model = tf.keras.Model(inputs=[xa_xb, xb, xc],
outputs=p_xc_given_xa_xb, name="inner_model")
model.compile(
optimizer=tf.optimizers.Adam(learning_rate=lr),
loss=neg_log_likelihood)
model.fit([Xa_train - Xb_train, Xb_train, Xc_train], ones_train,
validation_data=([Xa_test - Xb_test, Xb_test, Xc_test], ones_test),
epochs=1000,
verbose=True)
# After trained rebuild the part of the model I will use for prediction
xa_xb = model.get_layer('Xa-Xb')
L_xa_xb = model.get_layer('L_Xa-Xb')(xa_xb)
xb = model.get_layer('Xb')
L_xb = model.get_layer('L_Xb')(xb)
xc = model.get_layer('mean_vector')([L_xa_xb, L_xb])
model = tf.keras.Model(inputs=[xa_xb, xb],
outputs=xc, name="inner_model")
xc_hat = model([Xa_test - Xb_test, Xb_test])
The idea is to estimate xc However, I have the following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-57-d94b1e8a583c> in <module>()
2
3 xa_xb = model.get_layer('Xa-Xb')
----> 4 L_xa_xb = model.get_layer('L_Xa-Xb')(xa_xb)
5
6 xb = model.get_layer('Xb')
1 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
966 with base_layer_utils.autocast_context_manager(
967 self._compute_dtype):
--> 968 outputs = self.call(cast_inputs, *args, **kwargs)
969 self._handle_activity_regularization(inputs, outputs)
970 self._set_mask_metadata(inputs, outputs, input_masks)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py in call(self, inputs)
1178
1179 def call(self, inputs):
-> 1180 rank = inputs.shape.rank
1181 if rank is not None and rank > 2:
1182 # Broadcasting is required for the inputs.
AttributeError: 'InputLayer' object has no attribute 'shape'
Im usign Google Colaboratory
BATCH_SIZE =32
dataset = tf.data.Dataset.from_tensor_slices((question, ans)).shuffle(1000)
dataset = dataset.batch(BATCH_SIZE, drop_remainder=True)
class EncoderDecoder(tf.keras.Model):
def __init__(self,vocab_input=1000,vocab_output=1000,BATCH_SIZE=32):
super().__init__()
#Encoder
self.encoder_vector = tokens.TextVectorization(vocab_input,output_sequence_length=24)
self.encoder_embedding = tf.keras.layers.Embedding(vocab_input,256)
self.encoder_lstm = tf.keras.layers.LSTM(512,return_sequences=True)
self.attention = tf.keras.layers.Attention()
#Decoder
self.decoder_vector = tokens.TextVectorization(vocab_output,output_sequence_length=20)
self.decoder_embedding = tf.keras.layers.Embedding(vocab_output,256)
self.fc = tf.keras.layers.Dense(vocab_output)
self.decoder_lstm = tf.keras.layers.LSTM(512,return_sequences=True)
def loss_function(real, pred):
mask = tf.math.logical_not(tf.math.equal(real, 0))
loss_ = loss_object(real, pred)
mask = tf.cast(mask, dtype=loss_.dtype)
loss_ *= mask
return tf.reduce_mean(loss_)
def train_step(self,data):
loss =0
input = p[0]
targ = p[1]
input = self.encoder_vector(input)
targ = self.decoder_vector(targ)
with tf.GradientTape() as tape:
input = self.encoder_embedding(input)
enc_output,enc_h,enc_c = self.encoder_lstm(input)
attn_output = self.attention([enc_output,enc_h])
dec_h = tf.concat([tf.expand_dims(attn_output, 1), dec_h], axis=-1)
dec_input = tf.expand_dims([self.decoder_vector(['SOS'])]*BATCH_SIZE,1)
predictions = []
for t in range(1,targ.shape[1]):
dec_input = self.decoder_embedding(dec_input)
dec_output,dec_h,dec_c = self.decoder_lstm(dec_input,initial_states=[dec_h,enc_c])
predictions = self.fc(dec_output)
loss+=loss_function(targ[:,t],predictions)
dec_input = tf.expand_dims(targ[:, t], 1)
gradients = tape.gradient(loss,trainable_variables)
self.optimezer.apply_gradients(zip(gradients,trainable_variables))
self.compiled_metrics.update_state(targ,predictions)
return {m.name:m.result() for m in self.metrics}
model = EncoderDecoder()
model.compile(optimizer = 'adam',metrics = [tf.keras.metrics.SparseCategoricalCrossentropy()])
model.fit(dataset,epochs=10)
Epoch 1/10
---------------------------------------------------------------------------
StagingError Traceback (most recent call last)
<ipython-input-153-573fadf7e010> in <module>()
----> 1 model.fit(dataset,epochs=10)
10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
966 except Exception as e: # pylint:disable=broad-except
967 if hasattr(e, "ag_error_metadata"):
--> 968 raise e.ag_error_metadata.to_exception(e)
969 else:
970 raise
StagingError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:571 train_function *
outputs = self.distribute_strategy.run(
<ipython-input-151-46787da81d42>:33 train_step *
input = self.encoder_vector(input)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:897 __call__ **
self._maybe_build(inputs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:2416 _maybe_build
self.build(input_shapes) # pylint:disable=not-callable
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/preprocessing/text_vectorization.py:528 build
if self._split is not None and not input_shape[1] == 1: # pylint: disable=g-comparison-negation
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py:870 __getitem__
return self._dims[key].value
IndexError: list index out of range
I am trying to override the keras model train_step in my custom class.
question is set of English text questions and ans is their respective answers. They are of random length and are not padded as TextVectorization will add padding. So dataset contains question answer pair. Now I batch this and use model.fit.
The error is when assigning input = data[0] and targ = data[1] in train_step. I need help on how to assign the input and output in the train step for a batch.
I am using tf eager mode, and trying to create a GAN model. To made this, i created a class as follows. I tried sending array specificly, found in keras issues, but that also didn't worked?
class vanillaGAN(tf.keras.Model):
"""Vanilla GAN"""
def __init__(self, noise_dims, input_dims):
"""Define all layer used in network"""
super(vanillaGAN, self).__init__()
self.disc1 = tf.keras.layers.Dense(128, activation='relu')
self.disc2 = tf.keras.layers.Dense(1)#, activation='sigmoid')
self.gen1 = tf.keras.layers.Dense(128, activation='relu')
self.gen2 = tf.keras.layers.Dense(784)#, activation='sigmoid')
def gen_forward(self, x):
"""Forward Pass for Generator"""
x = self.gen1(x)
x = self.gen2(x)
return x
def dis_forward(self, x):
"""Forward Pass for Discriminator"""
x = self.disc1(x)
x = self.disc2(x)
return x
Now, on using following script:
def sample(batch_size, dims):
return np.random.uniform(size=(batch_size, dims))
gan = vanillaGAN(noise_dims=40, input_dims=784)
noise = sample(32,40)
#gan.gen_forward(np.array(noise))
gan.gen_forward(noise)}
I am getting following error
----------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-43-11c01bb2233d> in <module>
1 noise = sample(32,40)
----> 2 gan.gen_forward(np.array(noise))
<ipython-input-20-22ce18fda8ff> in gen_forward(self, x)
12 def gen_forward(self, x):
13 """Forward Pass for Generator"""
---> 14 x = self.gen1(x)
15 x = self.gen2(x)
16 return x
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
728
729 # Check input assumptions set before layer building, e.g. input rank.
--> 730 self._assert_input_compatibility(inputs)
731 if input_list and self._dtype is None:
732 try:
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in _assert_input_compatibility(self, inputs)
1461 spec.min_ndim is not None or
1462 spec.max_ndim is not None):
-> 1463 if x.shape.ndims is None:
1464 raise ValueError('Input ' + str(input_index) + ' of layer ' +
1465 self.name + ' is incompatible with the layer: '
AttributeError: 'tuple' object has no attribute 'ndims'
please, if someone can help.
Note that the model input should be a tensor, so running a model would be like:
gan = vanillaGAN(noise_dims=40, input_dims=784)
noise = sample(32,40)
# define the tensors
noise_tensor = tf.placeholder(tf.float32, shape=[32,40])
gen_output = gan.gen_forward(noise_tensor)
dis_output = gan.dis_forward(noise_tensor)
# define the initializer
# Ref: https://stackoverflow.com/questions/45139423/tensorflow-error-failedpeconditionerror-attempting-to-use-uninitialized-variab
init = tf.global_variables_initializer()
# run the graph
with tf.Session() as sess:
sess.run(init)
gen_output = sess.run(gen_output, feed_dict = {noise_tensor:noise})
dis_output = sess.run(dis_output, feed_dict = {noise_tensor:noise})
print(gen_output.shape)
print(dis_output.shape)
The error message is saying that numpy array doesn't have the attribute xxx.shape.ndims.
Experiment:
Access xxx.shape.ndims of a numpy array by noise.shape.ndims:
Traceback (most recent call last):
File "", line 1, in
noise.shape.ndims
AttributeError: 'tuple' object has no attribute 'ndims'
Access xxx.shape.ndims of a tensor by noise_tensor.shape.ndims:
2