I would like to implement a convolutional recurrent neural net with lstm in keras. I am a only a beginner in Machine Learning therefore I struggle understanding everything. Here is my code:
def initialize_model():
'Function to initialize the Convolutional Neural Network'
seed=123
# define CNN model
model = Sequential()
model.add(TimeDistributed(Conv2D(input_shape=(None,1,2048,1),kernel_size=(256,1),strides=(32,1),filters=16
,activation=LeakyReLU(0.01),data_format='channels_first')))
#cnn.add(MaxPooling2D(...))
model.add(TimeDistributed(Flatten()))
# define LSTM model
#model = Sequential()
#model.add(TimeDistribu(cnn))
model.add(LSTM(1024,recurrent_activation=LeakyReLU(0.01),recurrent_initializer=glorot_normal(seed)
,recurrent_regularizer=regularizers.l2(0.005)))
model.add(Dense(2048))
return model
def compile_model(model,learning_rate,loss_function):
#optimizer
optimizer = Adam(lr=learning_rate,beta_1=0.9,beta_2=0.999,epsilon=1e-8)
#Compile the model
model.compile(optimizer=optimizer,loss=loss_function,metrics=["accuracy"])
return model
# import and split data
data = get_data_from_files(10)
print(data.shape)
X_train, X_test, y_train, y_test = train_test_split(data[0],data[1], test_size=0.2, random_state=123)
print(X_train.shape) -> 160000,2048
print(y_train.shape) -> 160000,2048
print(X_test.shape) -> 40000,2048
print(y_test.shape) -> 40000,2048
X_tr = np.expand_dims(np.expand_dims(X_train,axis=2),axis=3)
X_te = np.expand_dims(np.expand_dims(X_test,axis=2),axis=3)
#X_tr shape -->(160000, 2048, 1, 1)
#X_te shape -->(40000, 2048, 1, 1)
model = initialize_model()
model =compile_model(model,0.001,"cosine_proximity")
hist = model.fit(X_tr,y_train
,validation_data=[X_te,y_test]
,epochs=5,batch_size=1000)
It returns the following error:
ValueError Traceback (most recent call last)
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in merge_with(self, other)
666 try:
--> 667 self.assert_same_rank(other)
668 new_dims = []
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in assert_same_rank(self, other)
711 raise ValueError("Shapes %s and %s must have the same rank" % (self,
--> 712 other))
713
ValueError: Shapes (256, 1, 2048, 16) and (?, ?, ?) must have the same rank
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in with_rank(self, rank)
741 try:
--> 742 return self.merge_with(unknown_shape(ndims=rank))
743 except ValueError:
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in merge_with(self, other)
672 except ValueError:
--> 673 raise ValueError("Shapes %s and %s are not compatible" % (self, other))
674
ValueError: Shapes (256, 1, 2048, 16) and (?, ?, ?) are not compatible
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-36-1633649265a0> in <module>()
6 hist = model.fit(X_tr,y_train
7 ,validation_data=[X_te,y_test]
----> 8 ,epochs=5,batch_size=1000)
9
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
953 sample_weight=sample_weight,
954 class_weight=class_weight,
--> 955 batch_size=batch_size)
956 # Prepare validation data.
957 do_validation = False
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
674 # to match the value shapes.
675 if not self.inputs:
--> 676 self._set_inputs(x)
677
678 if y is not None:
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/engine/training.py in _set_inputs(self, inputs, outputs, training)
574 assert len(inputs) == 1
575 inputs = inputs[0]
--> 576 self.build(input_shape=(None,) + inputs.shape[1:])
577 return
578
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/engine/sequential.py in build(self, input_shape)
225 self.inputs = [x]
226 for layer in self._layers:
--> 227 x = layer(x)
228 self.outputs = [x]
229
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
458 # Actually call the layer,
459 # collecting output(s), mask(s), and shape(s).
--> 460 output = self.call(inputs, **kwargs)
461 output_mask = self.compute_mask(inputs, previous_mask)
462
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/layers/wrappers.py in call(self, inputs, training, mask)
246 inner_mask_shape = self._get_shape_tuple((-1,), mask, 2)
247 kwargs['mask'] = K.reshape(mask, inner_mask_shape)
--> 248 y = self.layer.call(inputs, **kwargs)
249 if hasattr(y, '_uses_learning_phase'):
250 uses_learning_phase = y._uses_learning_phase
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/layers/convolutional.py in call(self, inputs)
166 padding=self.padding,
167 data_format=self.data_format,
--> 168 dilation_rate=self.dilation_rate)
169 if self.rank == 3:
170 outputs = K.conv3d(
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in conv2d(x, kernel, strides, padding, data_format, dilation_rate)
3564 strides=strides,
3565 padding=padding,
-> 3566 data_format=tf_data_format)
3567
3568 if data_format == 'channels_first' and tf_data_format == 'NHWC':
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in convolution(input, filter, padding, strides, dilation_rate, name, data_format)
777 dilation_rate=dilation_rate,
778 name=name,
--> 779 data_format=data_format)
780 return op(input, filter)
781
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in __init__(self, input_shape, filter_shape, padding, strides, dilation_rate, name, data_format)
854 filter_shape=filter_shape,
855 spatial_dims=spatial_dims,
--> 856 data_format=data_format)
857
858 def _build_op(self, _, padding):
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in __init__(self, input_shape, dilation_rate, padding, build_op, filter_shape, spatial_dims, data_format)
437 raise ValueError("dilation_rate must be positive")
438 if np.all(const_rate == 1):
--> 439 self.call = build_op(num_spatial_dims, padding)
440 return
441
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in _build_op(self, _, padding)
863 data_format=self.data_format,
864 strides=self.strides,
--> 865 name=self.name)
866
867 def __call__(self, inp, filter): # pylint: disable=redefined-builtin
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py in __init__(self, input_shape, filter_shape, padding, data_format, strides, name)
134 strides=None,
135 name=None):
--> 136 filter_shape = filter_shape.with_rank(input_shape.ndims)
137 self.padding = padding
138 self.name = name
~/PycharmProjects/Test/venv/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py in with_rank(self, rank)
742 return self.merge_with(unknown_shape(ndims=rank))
743 except ValueError:
--> 744 raise ValueError("Shape %s must have rank %d" % (self, rank))
745
746 def with_rank_at_least(self, rank):
ValueError: Shape (256, 1, 2048, 16) must have rank 3
I am completely stuck and have no idea how to resolve the problem.
EDIT:
I updated my code with the new error which is now a error of shape.
Fo anyone with a similar problem, I changed the trained and test data shape as follow :
X_tr = np.expand_dims(np.expand_dims(np.expand_dims(X_train,axis=1),axis=3),axis=1)
X_te = np.expand_dims(np.expand_dims(np.expand_dims(X_test,axis=1),axis=3),axis=1)
and the first layer :
model.add(TimeDistributed(Conv2D(kernel_size=(256,1),strides=(32,1),filters=16,activation=LeakyReLU(0.01),data_format='channels_first'),input_shape=(None,1,2048,1)))
It know run very well.
I am fairly confident that I have found the solution so this answer is edited to reflect that. According to keras documentation, when ising TimeDistribution with conv2d the input_shape must be a touple of length 4
Below is a part of the keras documentation.
TimeDistributed can be used with arbitrary layers, not just Dense, for instance with a Conv2D layer:
model = Sequential()
model.add(TimeDistributed(Conv2D(64, (3, 3)),
input_shape=(10, 299, 299, 3)))
Related
I am writing a code for deeplearning and I am currently trying to implement Focal Loss in my code, as my model isnt able to train itself in its current state. I am using a GPU. Whenever I run the code, I get the TypeError stated in the title. I have installed the focal loss package and am trying to implement it in my code with:
from focal_loss import BinaryFocalLoss
When I try to run my U-net and have my model train itself while using FocalLoss, I keep getting the error stated earlier. My U-net code is as follows:
class UNet(torch.nn.Module):
def __init__(self, n_classes=1, in_ch=1):
super().__init__()
# list of layers in encoder-decoder with number of filters
c = [16, 32, 64, 128]
# first conv layer that receives the image
self.conv1 = torch.nn.Sequential(
conv3x3_bn(in_ch, c[0]),
conv3x3_bn(c[0], c[0]),
)
# encoder layers
self.conv2 = encoder_conv(c[0], c[1])
self.conv3 = encoder_conv(c[1], c[2])
self.conv4 = encoder_conv(c[2], c[3])
# decoder layers
self.deconv1 = deconv(c[3],c[2])
self.deconv2 = deconv(c[2],c[1])
self.deconv3 = deconv(c[1],c[0])
# last layer conv that gives us the mask
self.out = torch.nn.Conv2d(c[0], n_classes, 3, padding=1)
def forward(self, x):
# encoder
x1 = self.conv1(x)
x2 = self.conv2(x1)
x3 = self.conv3(x2)
x = self.conv4(x3)
# decoder
x = self.deconv1(x, x3)
x = self.deconv2(x, x2)
x = self.deconv3(x, x1)
x = self.out(x)
return x
def conv3x3_bn(ci, co):
return torch.nn.Sequential(
torch.nn.Conv2d(ci, co, 3, padding=1),
torch.nn.BatchNorm2d(co),
torch.nn.ReLU(inplace=True)
)
def encoder_conv(ci, co):
return torch.nn.Sequential(
torch.nn.MaxPool2d(2),
conv3x3_bn(ci, co),
conv3x3_bn(co, co),
)
class deconv(torch.nn.Module):
def __init__(self, ci, co):
super(deconv, self).__init__()
self.upsample = torch.nn.ConvTranspose2d(ci, co, 2, stride=2)
self.conv1 = conv3x3_bn(ci, co)
self.conv2 = conv3x3_bn(co, co)
# receives the output of the previous layer and the output of the stage
# corresponding encoder
def forward(self, x1, x2):
x1 = self.upsample(x1)
diffX = x2.size()[2] - x1.size()[2]
diffY = x2.size()[3] - x1.size()[3]
x1 = F.pad(x1, (diffX, 0, diffY, 0))
# we concatenate the tensors
x = torch.cat([x2, x1], dim=1)
x = self.conv1(x)
x = self.conv2(x)
return x
My DataSet class is as follows:
class Dataset(torch.utils.data.Dataset):
def __init__(self, X, y, n_classes=1):
self.X = X
self.y = y
self.n_classes = n_classes
def __len__(self):
return len(self.X)
def __getitem__(self, ix):
img = nib.load(self.X[ix]).get_data()
mask = nib.load(self.y[ix]).get_data()
mask[mask==2] = 0
img = torch.tensor(img).unsqueeze(0)
#mask = (np.arange(self.n_classes) == mask[...,None]).astype(np.float32)
#return img, torch.from_numpy(mask).permute(2,0,1)
mask = torch.tensor(np.uint8(mask)).unsqueeze(0)
return img, mask
The training model is as follows:
from tqdm import tqdm #allows us to output a smart progress bar
def fit(model, dataloader, epochs=50, lr=3e-4):
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
criterion = BinaryFocalLoss(gamma=2) #loss
#criterion = WeightedFocalLoss()
model.to(device) #move model to device, which is the GPU
hist = {'loss': [], 'iou': [], 'evaluation_loss': [], 'evaluation_iou': []}
for epoch in range(1, epochs+1):
bar = tqdm(dataloader['train']) #creates a smart progress bar for train data
train_loss, train_iou = [], [] #create empty lists that are to be filled
model.train()
for imgs, masks in bar: #training the model
imgs, masks = imgs.float().to(device), masks.float().to(device)
optimizer.zero_grad()
y_hat = model(imgs)
y_hat = (y_hat - torch.min(y_hat))/(torch.max(y_hat) - torch.min(y_hat))
#print(y_hat.shape, masks.shape, y_hat.type(), masks.type(), np.unique(y_hat.detach().cpu().numpy()), np.unique(masks.detach().cpu().numpy()))
#print(y_hat.device, masks.device)
#plt.imshow(np.unique(masks.cpu().numpy(), return_counts=True))
loss = criterion(y_hat, masks)
print('a lot of attributes')
loss.backward() #GRADIENT DECENT, adam optimizer
optimizer.step() #updating model with the new gradients
ious = iou(y_hat, masks)
train_loss.append(loss.item())
train_iou.append(ious)
bar.set_description(f"loss {np.mean(train_loss):.5f} iou {np.mean(train_iou):.5f}")
hist['loss'].append(np.mean(train_loss))
hist['iou'].append(np.mean(train_iou))
bar = tqdm(dataloader['evaluation']) #creates a smart progress bar for evaluation data
evaluation_loss, evaluation_iou = [], [] #create empty lists for evaluation loss and iou
model.eval()
with torch.no_grad(): #evaluate the model
for imgs, masks in bar:
imgs, masks = imgs.float().to(device), masks.float().to(device)
y_hat = model(imgs)
y_hat = (y_hat - torch.min(y_hat))/(torch.max(y_hat) - torch.min(y_hat))
loss = criterion(y_hat, masks)
ious = iou(y_hat, masks)
evaluation_loss.append(loss.item())
evaluation_iou.append(ious)
bar.set_description(f"evaluation_loss {np.mean(evaluation_loss):.5f} evaluation_iou {np.mean(evaluation_iou):.5f}")
hist['evaluation_loss'].append(np.mean(evaluation_loss))
hist['evaluation_iou'].append(np.mean(evaluation_iou))
print(f"\nEpoch {epoch}/{epochs} loss {np.mean(train_loss):.5f} iou {np.mean(train_iou):.5f} evaluation_loss {np.mean(evaluation_loss):.5f} evaluation_iou {np.mean(evaluation_iou):.5f}")
return hist
When I run:
model = UNet()
hist = fit(model, dataloader, epochs=50)
I keep getting the following output & error:
TypeError Traceback (most recent call last)
~/tmp/ipykernel_84346/4198189162.py in <module>
1 model = UNet()
----> 2 hist = fit(model, dataloader, epochs=50)
~/tmp/ipykernel_84346/2960851020.py in fit(model, dataloader, epochs, lr)
19 #print(y_hat.device, masks.device)
20 #plt.imshow(np.unique(masks.cpu().numpy(), return_counts=True))
---> 21 loss = criterion(y_hat, masks)
22 print('a lot of attributes')
23 loss.backward() #GRADIENT DECENT, adam optimizer
~/.local/lib/python3.7/site-packages/keras/losses.py in __call__(self, y_true, y_pred, sample_weight)
139 else:
140 call_fn = tf.__internal__.autograph.tf_convert(self.call, tf.__internal__.autograph.control_status_ctx())
--> 141 losses = call_fn(y_true, y_pred)
142 return losses_utils.compute_weighted_loss(
143 losses, sample_weight, reduction=self._get_reduction())
~/.local/lib/python3.7/site-packages/focal_loss/_binary_focal_loss.py in call(self, y_true, y_pred)
394 pos_weight=self.pos_weight,
395 from_logits=self.from_logits,
--> 396 label_smoothing=self.label_smoothing)
397
398
~/.local/lib/python3.7/site-packages/focal_loss/_binary_focal_loss.py in binary_focal_loss(y_true, y_pred, gamma, pos_weight, from_logits, label_smoothing)
250 # Ensure predictions are a floating point tensor; converting labels to a
251 # tensor will be done in the helper functions
--> 252 y_pred = tf.convert_to_tensor(y_pred)
253 if not y_pred.dtype.is_floating:
254 y_pred = tf.dtypes.cast(y_pred, dtype=tf.float32)
~/.local/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
204 """Call target, and fall back on dispatchers if there is a TypeError."""
205 try:
--> 206 return target(*args, **kwargs)
207 except (TypeError, ValueError):
208 # Note: convert_to_eager_tensor currently raises a ValueError, not a
~/.local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor_v2_with_dispatch(value, dtype, dtype_hint, name)
1429 """
1430 return convert_to_tensor_v2(
-> 1431 value, dtype=dtype, dtype_hint=dtype_hint, name=name)
1432
1433
~/.local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor_v2(value, dtype, dtype_hint, name)
1439 name=name,
1440 preferred_dtype=dtype_hint,
-> 1441 as_ref=False)
1442
1443
~/.local/lib/python3.7/site-packages/tensorflow/python/profiler/trace.py in wrapped(*args, **kwargs)
161 with Trace(trace_name, **trace_kwargs):
162 return func(*args, **kwargs)
--> 163 return func(*args, **kwargs)
164
165 return wrapped
~/.local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
1564
1565 if ret is None:
-> 1566 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1567
1568 if ret is NotImplemented:
~/.local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
344 as_ref=False):
345 _ = as_ref
--> 346 return constant(v, dtype=dtype, name=name)
347
348
~/.local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name)
270 """
271 return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 272 allow_broadcast=True)
273
274
~/.local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
281 with trace.Trace("tf.constant"):
282 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
--> 283 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
284
285 g = ops.get_default_graph()
~/.local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
306 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
307 """Creates a constant on the current device."""
--> 308 t = convert_to_eager_tensor(value, ctx, dtype)
309 if shape is None:
310 return t
~/.local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
104 dtype = dtypes.as_dtype(dtype).as_datatype_enum
105 ctx.ensure_initialized()
--> 106 return ops.EagerTensor(value, ctx.device_name, dtype)
107
108
~/.local/lib/python3.7/site-packages/torch/_tensor.py in __array__(self, dtype)
641 return handle_torch_function(Tensor.__array__, (self,), self, dtype=dtype)
642 if dtype is None:
--> 643 return self.numpy()
644 else:
645 return self.numpy().astype(dtype, copy=False)
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
I have been stuck with this problem for 3 days and I cant seem to find a solution. Can someone please help me out?
The output, after changing device to cpu, has become:
RuntimeError Traceback (most recent call last)
~/tmp/ipykernel_56578/1031642734.py in <module>
1 model = UNet()
----> 2 hist = fit(model, dataloader, epochs=5)
~/tmp/ipykernel_56578/2559002388.py in fit(model, dataloader, epochs, lr)
19 #print(y_hat.device, masks.device)
20 #plt.imshow(np.unique(masks.cpu().numpy(), return_counts=True))
---> 21 loss = criterion(y_hat, masks)
22 print('a lot of attributes')
23 loss.backward() #GRADIENT DECENT, adam optimizer
~/.local/lib/python3.7/site-packages/keras/losses.py in __call__(self, y_true, y_pred, sample_weight)
139 else:
140 call_fn = tf.__internal__.autograph.tf_convert(self.call, tf.__internal__.autograph.control_status_ctx())
--> 141 losses = call_fn(y_true, y_pred)
142 return losses_utils.compute_weighted_loss(
143 losses, sample_weight, reduction=self._get_reduction())
~/.local/lib/python3.7/site-packages/focal_loss/_binary_focal_loss.py in call(self, y_true, y_pred)
394 pos_weight=self.pos_weight,
395 from_logits=self.from_logits,
--> 396 label_smoothing=self.label_smoothing)
397
398
~/.local/lib/python3.7/site-packages/focal_loss/_binary_focal_loss.py in binary_focal_loss(y_true, y_pred, gamma, pos_weight, from_logits, label_smoothing)
264 return _binary_focal_loss_from_probs(labels=y_true, p=y_pred,
265 gamma=gamma, pos_weight=pos_weight,
--> 266 label_smoothing=label_smoothing)
267
268
~/.local/lib/python3.7/site-packages/focal_loss/_binary_focal_loss.py in _binary_focal_loss_from_probs(labels, p, gamma, pos_weight, label_smoothing)
556 # Combine loss terms
557 if label_smoothing is None:
--> 558 labels = tf.dtypes.cast(labels, dtype=tf.bool)
559 loss = tf.where(labels, pos_loss, neg_loss)
560 else:
~/.local/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
204 """Call target, and fall back on dispatchers if there is a TypeError."""
205 try:
--> 206 return target(*args, **kwargs)
207 except (TypeError, ValueError):
208 # Note: convert_to_eager_tensor currently raises a ValueError, not a
~/.local/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py in cast(x, dtype, name)
986 # allows some conversions that cast() can't do, e.g. casting numbers to
987 # strings.
--> 988 x = ops.convert_to_tensor(x, name="x")
989 if x.dtype.base_dtype != base_type:
990 x = gen_math_ops.cast(x, base_type, name=name)
~/.local/lib/python3.7/site-packages/tensorflow/python/profiler/trace.py in wrapped(*args, **kwargs)
161 with Trace(trace_name, **trace_kwargs):
162 return func(*args, **kwargs)
--> 163 return func(*args, **kwargs)
164
165 return wrapped
~/.local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
1564
1565 if ret is None:
-> 1566 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1567
1568 if ret is NotImplemented:
~/.local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
344 as_ref=False):
345 _ = as_ref
--> 346 return constant(v, dtype=dtype, name=name)
347
348
~/.local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name)
270 """
271 return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 272 allow_broadcast=True)
273
274
~/.local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
281 with trace.Trace("tf.constant"):
282 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
--> 283 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
284
285 g = ops.get_default_graph()
~/.local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
306 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
307 """Creates a constant on the current device."""
--> 308 t = convert_to_eager_tensor(value, ctx, dtype)
309 if shape is None:
310 return t
~/.local/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
104 dtype = dtypes.as_dtype(dtype).as_datatype_enum
105 ctx.ensure_initialized()
--> 106 return ops.EagerTensor(value, ctx.device_name, dtype)
107
108
~/.local/lib/python3.7/site-packages/torch/_tensor.py in __array__(self, dtype)
641 return handle_torch_function(Tensor.__array__, (self,), self, dtype=dtype)
642 if dtype is None:
--> 643 return self.numpy()
644 else:
645 return self.numpy().astype(dtype, copy=False)
RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.
The rest of the code is the same.
I'm just working through my own sandbox project wanting to try and implement NLP but with a linear regression as an outcome. As reference, the dataset I am working with comes Kaggle wine-reviews which has the wine reviews and a corresponding score of 1 through 100 hence why I'm using linear regression instead of classification.
But I am getting an error message and I'm not sure if it's a result of a data type or dimensionality problem, and I'm not sue why or how to resolve it.
I'll provide the code bellow, and some intermediate outcomes displaying the dimensions of some of the objects as I'm assuming that that might be useful in solving this.
df = pd.read_csv('winemag-data_first150k.csv', encoding='ISO-8859-1')
y = df['points'].astype(int)
X = df['description'].astype(str)
# split up the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
MAX_VOCAB_SIZE = 35000
tokenizer = Tokenizer(num_words=MAX_VOCAB_SIZE)
tokenizer.fit_on_texts(X_train)
sequences_train = tokenizer.texts_to_sequences(X_train)
sequences_test = tokenizer.texts_to_sequences(X_test)
word2idx = tokenizer.word_index
V = len(word2idx)
print('Found %s unique tokens.' % V)
Found 33012 unique tokens.
data_train = pad_sequences(sequences_train)
print('Shape of data train tensor:', data_train.shape)
# get sequence length
T = data_train.shape[1]
Shape of data train tensor: (101123, 136)
data_test = pad_sequences(sequences_test, maxlen=T)
print('Shape of data test tensor:', data_test.shape)
Shape of data test tensor: (49807, 136)
# Create the model
# We get to choose embedding dimensionality
D = 20
# Hidden state dimensionality
M = 15
i = Input(shape=(T,))
x = Embedding(V + 1, D)(i)
x = LSTM(M, return_sequences=True)(x)
x = GlobalMaxPooling1D()(x)
x = Dense(1)(x)
model = Model(i, x)
model.compile(optimizer='adam', loss='mse')
# learning rate scheduler
def schedule(epoch, lr):
if epoch >= 50:
return 0.0001
return 0.001
scheduler = tf.keras.callbacks.LearningRateScheduler(schedule)
# Train the model
r = model.fit(X, y, epochs=200, callbacks=[scheduler])
And then I get the error message along with a warning about dimensionality:
Epoch 1/200
WARNING:tensorflow:Model was constructed with shape (None, 136) for input Tensor("input_10:0", shape=(None, 136), dtype=float32), but it was called on an input with incompatible shape (None, 1).
WARNING:tensorflow:Model was constructed with shape (None, 136) for input Tensor("input_10:0", shape=(None, 136), dtype=float32), but it was called on an input with incompatible shape (None, 1).
---------------------------------------------------------------------------
UnimplementedError Traceback (most recent call last)
<ipython-input-121-0f68916ec23b> in <module>
13
14 # Train the model
---> 15 r = model.fit(X, y, epochs=200, callbacks=[scheduler])
~\anaconda3\envs\newenvt\lib\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, *args, **kwargs)
106 def _method_wrapper(self, *args, **kwargs):
107 if not self._in_multi_worker_mode(): # pylint: disable=protected-access
--> 108 return method(self, *args, **kwargs)
109
110 # Running inside `run_distribute_coordinator` already.
~\anaconda3\envs\newenvt\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1096 batch_size=batch_size):
1097 callbacks.on_train_batch_begin(step)
-> 1098 tmp_logs = train_function(iterator)
1099 if data_handler.should_sync:
1100 context.async_wait()
~\anaconda3\envs\newenvt\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
778 else:
779 compiler = "nonXla"
--> 780 result = self._call(*args, **kwds)
781
782 new_tracing_count = self._get_tracing_count()
~\anaconda3\envs\newenvt\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
838 # Lifting succeeded, so variables are initialized and we can run the
839 # stateless function.
--> 840 return self._stateless_fn(*args, **kwds)
841 else:
842 canon_args, canon_kwds = \
~\anaconda3\envs\newenvt\lib\site-packages\tensorflow\python\eager\function.py in __call__(self, *args, **kwargs)
2827 with self._lock:
2828 graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
-> 2829 return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access
2830
2831 #property
~\anaconda3\envs\newenvt\lib\site-packages\tensorflow\python\eager\function.py in _filtered_call(self, args, kwargs, cancellation_manager)
1841 `args` and `kwargs`.
1842 """
-> 1843 return self._call_flat(
1844 [t for t in nest.flatten((args, kwargs), expand_composites=True)
1845 if isinstance(t, (ops.Tensor,
~\anaconda3\envs\newenvt\lib\site-packages\tensorflow\python\eager\function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
1921 and executing_eagerly):
1922 # No tape is watching; skip to running the function.
-> 1923 return self._build_call_outputs(self._inference_function.call(
1924 ctx, args, cancellation_manager=cancellation_manager))
1925 forward_backward = self._select_forward_and_backward_functions(
~\anaconda3\envs\newenvt\lib\site-packages\tensorflow\python\eager\function.py in call(self, ctx, args, cancellation_manager)
543 with _InterpolateFunctionError(self):
544 if cancellation_manager is None:
--> 545 outputs = execute.execute(
546 str(self.signature.name),
547 num_outputs=self._num_outputs,
~\anaconda3\envs\newenvt\lib\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
57 try:
58 ctx.ensure_initialized()
---> 59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
UnimplementedError: Cast string to float is not supported
[[node functional_11/Cast (defined at <ipython-input-121-0f68916ec23b>:15) ]] [Op:__inference_train_function_17206]
Function call stack:
train_function
I'm not entirely sure what needs to be changed, but any suggestions would be appreciated.
From comments
Passing X to model.fit, which literally has string values, a neural
network cannot be input string values (paraphrased from Dr. Snoopy)
I am trying to build a vector quantized variational autoencoder in tensorflow. A very cryptic error occurs when I build my model and start training.
I have gone through the code to find an mistakes but I could not find one. The error is very unhelpful in finding the cause. Below is part of the code for model
def call(self, inputs):
z = self.encode(inputs)
vq_output = self.vector_quantizer(z)
reconstructed_image = self.decode(vq_output['quantize'])
return reconstructed_image, vq_output
def encode(self, image):
encoded = Conv2D(int(self._num_hidden/2), kernel_size=(4,4), strides=(2,2), activation='relu', name='enc1')(image)
encoded = Conv2D(self._num_hidden, kernel_size=(4,4), strides=(2,2), activation='relu', name='enc2')(encoded)
encoded = Conv2D(self._num_hidden, kernel_size=(3,3), strides=(1,1), name='enc2')(encoded)
encoded = residual_stack(encoded, self._num_hidden, self._num_residual_layers, self._num_residual_hiddens)
return Conv2D(self._embeding_dim, kernel_size=(1,1) ,strides=(1,1), name='enc3')(encoded)
def decode(self, encoded_input):
decoded = Conv2D(self._decoder_num_hidden, kernel_size=(3,3), strides=(1,1), name='dec1')(encoded_input)
decoded = residual_stack(decoded, self._decoder_num_hidden, self._decoder_num_residual_layers, self._decoder_num_residual_hiddens)
decoded = Conv2DTranspose(int(self._decoder_num_hidden/2), kernel_size=(4,4), strides=(2,2), activation='relu', name='dec2')(decoded)
return Conv2DTranspose(3, kernel_size=(4,4), strides=(2,2), name='dec3')(decoded)
The following error occurs:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-52-90a0a1fb38f6> in <module>()
15
16 for batch in train_batches:
---> 17 _loss, _perplexity = train(vq_vae, batch['images'], optimizer)
18 loss.update_state(_loss)
19 perplexity.update_state(_perplexity)
14 frames
<ipython-input-45-0025b05fe5c9> in train(model, inputs, optimizer, outputs)
1 def train(model, inputs, optimizer, outputs=None):
2 with tf.GradientTape() as tape:
----> 3 loss, perplexity = compute_loss(model, inputs)
4 gradients = tape.gradient(loss, model.trainable_variables)
5 optimizer.apply_gradients(zip(gradients, model.trainable_variables))
<ipython-input-44-242959fe043f> in compute_loss(model, inputs, outputs)
1 def compute_loss(model, inputs, outputs=None):
----> 2 recon, vq_outputs = model(inputs)
3 recon_loss = tf.reduce_mean((recon - inputs)**2) / data_variance
4 loss = recon_loss + vq_outputs['loss']
5 return loss, vq_outputs['perplexity']
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
677 with base_layer_utils.autocast_context_manager(
678 input_list, self._mixed_precision_policy.should_cast_variables):
--> 679 outputs = self.call(inputs, *args, **kwargs)
680 self._handle_activity_regularization(inputs, outputs)
681 self._set_mask_metadata(inputs, outputs, previous_mask)
<ipython-input-43-f28d8aaad600> in call(self, inputs)
40 vq_output = self.vector_quantizer(z)
41
---> 42 reconstructed_image = self.decode(vq_output['quantize'])
43
44 return reconstructed_image, vq_output
<ipython-input-43-f28d8aaad600> in decode(self, encoded_input)
55
56 def decode(self, encoded_input):
---> 57 decoded = Conv2D(self._decoder_num_hidden, kernel_size=(3,3), strides=(1,1), name='dec1')(encoded_input)
58
59 decoded = residual_stack(decoded, self._decoder_num_hidden, self._decoder_num_residual_layers, self._decoder_num_residual_hiddens)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
674 # Eager execution on data tensors.
675 with backend.name_scope(self._name_scope()):
--> 676 self._maybe_build(inputs)
677 with base_layer_utils.autocast_context_manager(
678 input_list, self._mixed_precision_policy.should_cast_variables):
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
1879 # operations.
1880 with tf_utils.maybe_init_scope(self):
-> 1881 self.build(input_shapes)
1882 # We must set self.built since user defined build functions are not
1883 # constrained to set self.built.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/convolutional.py in build(self, input_shape)
163 constraint=self.kernel_constraint,
164 trainable=True,
--> 165 dtype=self.dtype)
166 if self.use_bias:
167 self.bias = self.add_weight(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint, partitioner, use_resource, synchronization, aggregation, **kwargs)
382 collections=collections_arg,
383 synchronization=synchronization,
--> 384 aggregation=aggregation)
385 backend.track_variable(variable)
386
/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/tracking/base.py in _add_variable_with_custom_getter(self, name, shape, dtype, initializer, getter, overwrite, **kwargs_for_getter)
661 dtype=dtype,
662 initializer=initializer,
--> 663 **kwargs_for_getter)
664
665 # If we set an initializer and the variable processed it, tracking will not
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer_utils.py in make_variable(name, shape, dtype, initializer, trainable, caching_device, validate_shape, constraint, use_resource, collections, synchronization, aggregation, partitioner)
140 # TODO(apassos,rohanj) figure out how to remove collections from here so we
141 # can remove the V1.
--> 142 variable_shape = tensor_shape.TensorShape(shape)
143 return tf_variables.VariableV1(
144 initial_value=init_val,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in __init__(self, dims)
772 else:
773 # Got a list of dimensions
--> 774 self._dims = [as_dimension(d) for d in dims_iter]
775
776 #property
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in <listcomp>(.0)
772 else:
773 # Got a list of dimensions
--> 774 self._dims = [as_dimension(d) for d in dims_iter]
775
776 #property
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in as_dimension(value)
714 return value
715 else:
--> 716 return Dimension(value)
717
718
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in __init__(self, value)
183 raise TypeError("Cannot convert %s to Dimension" % value)
184 else:
--> 185 self._value = int(value)
186 if (not isinstance(value, compat.bytes_or_text_types) and
187 self._value != value):
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'
I cannot understand where I made a mistake by looking at this error.
I am trying to train variational encoder. But I am getting
InvalidArgumentError: Incompatible shapes: [32,784] vs. [32,2352]
[[{{node custom_variational_layer_21/logistic_loss/mul}}]].
I read the images using opencv and append it to the list and then I converted it into numpy array.
Copied code from : http://www.stokastik.in/understanding-variational-autoencoders/
I am using convolutional variational autoencoder.
images = []
files = glob.glob('../dataset/maggi/*.*')
i=0
for file in files:
try:
img = cv2.imread(file)
img = cv2.resize(img, (28,28))
images.append(img)
except:
print('error')
x_train = np.asarray(images)
x_train = x_train.astype('float32') / 255.
print('Input size : ',x_train.shape)
conv_variational_autoencoder(x_train)
Output :
Input size : (1446, 28, 28, 3)
Epoch 1/50
----------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-166-2e8711de7bdc> in <module>()
72 print('Input size : ',x_train.shape)
73
---> 74 conv_variational_autoencoder(x_train)
<ipython-input-166-2e8711de7bdc> in conv_variational_autoencoder(X_train)
50 adam = Adam(lr=0.0005)
51 autoencoder.compile(optimizer=adam, loss=None)
---> 52 autoencoder.fit(X_train, shuffle=True, epochs=50, batch_size=32)
53
54
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1037 initial_epoch=initial_epoch,
1038 steps_per_epoch=steps_per_epoch,
-> 1039 validation_steps=validation_steps)
1040
1041 def evaluate(self, x=None, y=None,
/usr/local/lib/python3.6/dist-packages/keras/engine/training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
197 ins_batch[i] = ins_batch[i].toarray()
198
--> 199 outs = f(ins_batch)
200 outs = to_list(outs)
201 for l, o in zip(out_labels, outs):
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
2713 return self._legacy_call(inputs)
2714
-> 2715 return self._call(inputs)
2716 else:
2717 if py_any(is_tensor(x) for x in inputs):
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _call(self, inputs)
2673 fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)
2674 else:
-> 2675 fetched = self._callable_fn(*array_vals)
2676 return fetched[:len(self.outputs)]
2677
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1437 ret = tf_session.TF_SessionRunCallable(
1438 self._session._session, self._handle, args, status,
-> 1439 run_metadata_ptr)
1440 if run_metadata:
1441 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
526 None, None,
527 compat.as_text(c_api.TF_Message(self.status.status)),
--> 528 c_api.TF_GetCode(self.status.status))
529 # Delete the underlying status object from memory otherwise it stays alive
530 # as there is a reference to status from this from the traceback due to
InvalidArgumentError: Incompatible shapes: [32,784] vs. [32,2352]
[[{{node custom_variational_layer_21/logistic_loss/mul}}]]
Thanks for the link to the article! It's a really interesting and good writeup.
Now for the problem:
As a rule: ALWAYS check your models' inputs and ouputs by using the model.summaray() function. In your case your model looks like this:
Now watch closely. Your input images are of the shape 28x28x3 like you defined yourself. But the output is 28x28x1 because the article you used trains the model on mnist, which is greyscale and thus only has 1 channel for colors, you have three.
This yields an error in the loss function, because it tries to compare how well a greyscale image looks like a color image, which of course doesn't work.
To fix this, all you have to do is go to the decoder part of the conv_variational_autoencoder(x_train) function and change the output size of the last Conv2DTranspose to be 28x28x3 instead of 28x28x1:
#Decoder
decoder_input = Input(shape=(196,))
p = Reshape((14, 14, 1))(decoder_input)
x = Conv2DTranspose(32, (3, 3), activation='relu', padding='same')(p)
x = UpSampling2D((2, 2))(x)
# dec_out = Conv2DTranspose(1, (3, 3), activation='sigmoid', padding='same')(x)
# Change the above line to:
dec_out = Conv2DTranspose(3, (3, 3), activation='sigmoid', padding='same')(x)
decoder = Model(decoder_input, dec_out)
And it should train straight away. Good luck!
I am very new to machine learning and I came across an error while attempting to make a simple LSTM model, and I am absolutely clueless how to debug this. I am using Keras version 2.2.2.
My code looks more or less like this:
model = Sequential()
model.add(Embedding(400001, emb_dim, trainable=False, input_length = 56, weights = [emb_matrix]))
model.add(LSTM(128, return_sequences=False))
model.add(Dense(5, activation='softmax'))
model.summary()
model.fit(train_in, train_out, epochs = 50, batch_size = 32, shuffle=True)
My inputs were initially lists of sentences that I intend to do sentiment analysis on, I then use Glove vectors with 50 dim to convert the sentences into vectors with shape (sample size, 56, 50), since my maximum number of words per sentence is 56 (is this on the high side?).
My model summary:
Layer (type) Output Shape Param #
=================================================================
embedding_5 (Embedding) (None, 56, 50) 20000050
_________________________________________________________________
lstm_6 (LSTM) (None, 128) 91648
_________________________________________________________________
dense_4 (Dense) (None, 5) 645
=================================================================
Total params: 20,092,343
Trainable params: 92,293
Non-trainable params: 20,000,050
My inputs:
print(train_in.shape, train_out.shape)
>(156060, 56) (156060, 5)
emb_matrix.shape
>(400001, 50)
print(train_in.dtype, train_out.dtype, emb_matrix.dtype)
>float32 float32 float32
And finally my error message:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\gradients_impl.py in _MaybeCompile(scope, op, func, grad_fn)
369 try:
--> 370 xla_compile = op.get_attr("_XlaCompile")
371 xla_separate_compiled_gradients = op.get_attr(
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\ops.py in get_attr(self, name)
2172 raise ValueError(
-> 2173 "No attr named '" + name + "' in " + str(self._node_def))
2174 x = self._node_def.attr[name]
ValueError: No attr named '_XlaCompile' in name: "lstm_6/while/TensorArrayWrite/TensorArrayWriteV3"
op: "TensorArrayWriteV3"
input: "lstm_6/while/TensorArrayWrite/TensorArrayWriteV3/Enter"
input: "lstm_6/while/Identity_1"
input: "lstm_6/while/mul_5"
input: "lstm_6/while/Identity_2"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
attr {
key: "_class"
value {
list {
s: "loc:#lstm_6/while/mul_5"
}
}
}
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
509 as_ref=input_arg.is_ref,
--> 510 preferred_dtype=default_dtype)
511 except TypeError as err:
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
1021 if ret is None:
-> 1022 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1023
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\ops.py in _TensorTensorConversionFunction(t, dtype, name, as_ref)
865 "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" %
--> 866 (dtype.name, t.dtype.name, str(t)))
867 return t
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype int64: 'Tensor("lstm_6/while/maximum_iterations:0", shape=(), dtype=int64)'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-54-936a1189c2d5> in <module>()
----> 1 model.fit(train_in, train_out, epochs = 50, batch_size = 32, shuffle=True)
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1006 else:
1007 ins = x + y + sample_weights
-> 1008 self._make_train_function()
1009 f = self.train_function
1010
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\keras\engine\training.py in _make_train_function(self)
496 training_updates = self.optimizer.get_updates(
497 params=self._collected_trainable_weights,
--> 498 loss=self.total_loss)
499 updates = (self.updates +
500 training_updates +
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name +
90 '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\keras\optimizers.py in get_updates(self, loss, params)
633 #interfaces.legacy_get_updates_support
634 def get_updates(self, loss, params):
--> 635 grads = self.get_gradients(loss, params)
636 self.updates = [K.update_add(self.iterations, 1)]
637
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\keras\optimizers.py in get_gradients(self, loss, params)
87
88 def get_gradients(self, loss, params):
---> 89 grads = K.gradients(loss, params)
90 if None in grads:
91 raise ValueError('An operation has `None` for gradient. '
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\keras\backend\tensorflow_backend.py in gradients(loss, variables)
2706 A gradients tensor.
2707 """
-> 2708 return tf.gradients(loss, variables, colocate_gradients_with_ops=True)
2709
2710
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\gradients_impl.py in gradients(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients)
607 # functions.
608 in_grads = _MaybeCompile(
--> 609 grad_scope, op, func_call, lambda: grad_fn(op, *out_grads))
610 else:
611 # For function call ops, we add a 'SymbolicGradient'
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\gradients_impl.py in _MaybeCompile(scope, op, func, grad_fn)
373 xla_scope = op.get_attr("_XlaScope").decode()
374 except ValueError:
--> 375 return grad_fn() # Exit early
376
377 if not xla_compile:
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\gradients_impl.py in <lambda>()
607 # functions.
608 in_grads = _MaybeCompile(
--> 609 grad_scope, op, func_call, lambda: grad_fn(op, *out_grads))
610 else:
611 # For function call ops, we add a 'SymbolicGradient'
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\tensor_array_grad.py in _TensorArrayWriteGrad(op, flow)
129 colocate_with_first_write_call=False)
130 .grad(source=grad_source, flow=flow))
--> 131 grad = g.read(index)
132 return [None, None, grad, flow]
133
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\tensor_array_ops.py in read(self, index, name)
857 The tensor at index `index`.
858 """
--> 859 return self._implementation.read(index, name=name)
860
861 #tf_should_use.should_use_result
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\tensor_array_ops.py in read(self, index, name)
257 flow_in=self._flow,
258 dtype=self._dtype,
--> 259 name=name)
260 if self._element_shape:
261 value.set_shape(self._element_shape[0].dims)
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\gen_data_flow_ops.py in _tensor_array_read_v3(handle, index, flow_in, dtype, name)
4993 _, _, _op = _op_def_lib._apply_op_helper(
4994 "TensorArrayReadV3", handle=handle, index=index, flow_in=flow_in,
-> 4995 dtype=dtype, name=name)
4996 _result = _op.outputs[:]
4997 _inputs_flat = _op.inputs
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
785 op = g.create_op(op_type_name, inputs, output_types, name=scope,
786 input_types=input_types, attrs=attr_protos,
--> 787 op_def=op_def)
788 return output_structure, op_def.is_stateful, op
789
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\ops.py in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
3158 input_types=input_types,
3159 original_op=self._default_original_op,
-> 3160 op_def=op_def)
3161 self._create_op_helper(ret, compute_shapes=compute_shapes,
3162 compute_device=compute_device)
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\ops.py in __init__(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
1672 control_flow_util.CheckInputFromValidContext(self, input_tensor.op)
1673 if self._control_flow_context is not None:
-> 1674 self._control_flow_context.AddOp(self)
1675 self._recompute_node_def()
1676
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in AddOp(self, op)
2249 op_input_ctxt._AddOpInternal(op)
2250 return
-> 2251 self._AddOpInternal(op)
2252
2253 def _AddOpInternal(self, op):
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in _AddOpInternal(self, op)
2272 for index in range(len(op.inputs)):
2273 x = op.inputs[index]
-> 2274 real_x = self.AddValue(x)
2275 if real_x != x:
2276 op._update_input(index, real_x)
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in AddValue(self, val)
2205 forward_ctxt = forward_ctxt.GetWhileContext()
2206 if forward_ctxt == grad_ctxt.grad_state.forward_context:
-> 2207 real_val = grad_ctxt.grad_state.GetRealValue(val)
2208 self._external_values[val.name] = real_val
2209 return real_val
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in GetRealValue(self, value)
1048 # Record the history of this value in forward_ctxt.
1049 self._grad_context.Exit()
-> 1050 history_value = cur_grad_state.AddForwardAccumulator(cur_value)
1051 self._grad_context.Enter()
1052 break
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py in AddForwardAccumulator(self, value, dead_branch)
906 max_size=maximum_iterations,
907 elem_type=value.dtype.base_dtype,
--> 908 name="f_acc")
909 # pylint: enable=protected-access
910 if curr_ctxt: curr_ctxt.Exit()
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\gen_data_flow_ops.py in _stack_v2(max_size, elem_type, stack_name, name)
4014 _, _, _op = _op_def_lib._apply_op_helper(
4015 "StackV2", max_size=max_size, elem_type=elem_type,
-> 4016 stack_name=stack_name, name=name)
4017 _result = _op.outputs[:]
4018 _inputs_flat = _op.inputs
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
531 if input_arg.type != types_pb2.DT_INVALID:
532 raise TypeError("%s expected type of %s." %
--> 533 (prefix, dtypes.as_dtype(input_arg.type).name))
534 else:
535 # Update the maps with the default, if needed.
TypeError: Input 'max_size' of 'StackV2' Op has type int64 that does not match expected type of int32.
I was using version 1.5.0 of TF initially, upgraded to v1.8.0 and all is working. Issue resolved.