I'm trying to train / use a convolutional neural network with neupy library for a project, but I'm getting errors in the training phase.
I have many images (rgb, shape=66, 160, 3) and I split them in the training and test sets. Then I'm trying to train one convolutional neural network (I'll try to optimize with different algorithm, layer number and size later). The target output for my project is a number [-1, 1], I'm solving a regression problem but I have issues before.
The error I'm getting right now is:
ValueError: Cannot shuffle matrices. All matrices should have the same number of rows
The relevant code:
print numpy.array(y_train).shape
# outputs (84, 66, 160, 3)
print numpy.array(y_test).shape
# outputs (15, 66, 160, 3)
cgnet = algorithms.Adadelta(
[
layers.Input((6, 66, 160*3)),
layers.Convolution((8, 3, 3)),
layers.Relu(),
layers.Convolution((8, 3, 3)),
layers.Relu(),
layers.MaxPooling((2, 2)),
layers.Reshape(),
layers.Linear(1024),
layers.Softmax(10),
],
error='categorical_crossentropy',
step=1.0,
verbose=True,
shuffle_data=True,
#shuffle_data=False,
reduction_freq=8,
addons=[algorithms.StepDecay],
)
print cgnet.architecture()
cgnet.train(x_train, y_train, x_test, y_test, epochs=100)
Output:
Main information
[ALGORITHM] Adadelta
[OPTION] batch_size = 128
[OPTION] verbose = True
[OPTION] epoch_end_signal = None
[OPTION] show_epoch = 1
[OPTION] shuffle_data = True
[OPTION] step = 1.0
[OPTION] train_end_signal = None
[OPTION] error = categorical_crossentropy
[OPTION] addons = ['StepDecay']
[OPTION] decay = 0.95
[OPTION] epsilon = 1e-05
[OPTION] reduction_freq = 8
[THEANO] Initializing Theano variables and functions.
[THEANO] Initialization finished successfully. It took 7.01 seconds
Network's architecture
-------------------------------------------------
| # | Input shape | Layer Type | Output shape |
-------------------------------------------------
| 1 | (6, 66, 480) | Input | (6, 66, 480) |
| 2 | (6, 66, 480) | Convolution | (8, 64, 478) |
| 3 | (8, 64, 478) | Relu | (8, 64, 478) |
| 4 | (8, 64, 478) | Convolution | (8, 62, 476) |
| 5 | (8, 62, 476) | Relu | (8, 62, 476) |
| 6 | (8, 62, 476) | MaxPooling | (8, 31, 238) |
| 7 | (8, 31, 238) | Reshape | 59024 |
| 8 | 59024 | Linear | 1024 |
| 9 | 1024 | Softmax | 10 |
-------------------------------------------------
None
Start training
[TRAIN DATA] 84 samples, feature shape: (66, 160, 3)
[TEST DATA] 15 samples, feature shape: (66, 160, 3)
[TRAINING] Total epochs: 100
------------------------------------------------
| Epoch # | Train err | Valid err | Time |
------------------------------------------------
Traceback (most recent call last):
File "./ml_neupy.py", line 68, in <module>
cgnet.train(x_train, y_train, x_test, y_test, epochs=100)
File "/usr/local/lib/python2.7/dist-packages/neupy/algorithms/constructor.py", line 539, in train
*args, **kwargs
File "/usr/local/lib/python2.7/dist-packages/neupy/algorithms/learning.py", line 49, in train
summary=summary
File "/usr/local/lib/python2.7/dist-packages/neupy/algorithms/base.py", line 409, in train
target_train)
File "/usr/local/lib/python2.7/dist-packages/neupy/algorithms/utils.py", line 146, in shuffle
raise ValueError("Cannot shuffle matrices. All matrices should "
ValueError: Cannot shuffle matrices. All matrices should have the same number of rows
What is wrong with the input data or the network?
Thanks
There are a few things that you need to modify:
You've mentioned that you are trying to solve regression problem. Your network has a Softmax layer as the output, which means that your network can give you only outputs from [0, 1] range, instead of [-1, 1]. You can change it to Tanh layer. It will produce output from [-1, 1] range.
Cross entropy error suitable only for classification problems
error='categorical_crossentropy'
For regression you can use MSE or RMSE (more error functions you can find here)
error='mse'
I assume that in the (66, 160, 3) shape number 3 defines each of the RGB channels. NeuPy works with Theano library, which means that you need to define channel shape before the width and height of the image. The right order is: (n_samples, n_channels, height, width). In your case I assume that you have 84 samples, height of 66 pixels, width of 160 pixels and 3 channels (RGB). If that true then you need to transform your input as follows
# convert this shape (n_samples, height, width, n_channels)
# to (n_samples, n_channels, height, width)
x_train = x_train.transpose((0, 3, 1, 2))
print(x_train.shape) # (84, 3, 66, 160)
Output from the final layer should be 1 instead of 10. It means that you predict only one value per each sample, instead of vector with 10 values (use layers.Tanh(1) instead of layers.Softmax(10))
The following code works without errors (doesn't train properly, because values are random):
import numpy
from neupy import algorithms, layers
x_train = numpy.random.random((84, 3, 66, 160))
x_test = numpy.random.random((15, 3, 66, 160))
y_train = numpy.random.random(84)
y_test = numpy.random.random(15)
cgnet = algorithms.Adadelta(
[
layers.Input((3, 66, 160)),
layers.Convolution((8, 3, 3)),
layers.Relu(),
layers.Convolution((8, 3, 3)),
layers.Relu(),
layers.MaxPooling((2, 2)),
layers.Reshape(),
layers.Linear(1024),
layers.Tanh(1),
],
error='mse',
step=1.0,
verbose=True,
shuffle_data=True,
reduction_freq=8,
addons=[algorithms.StepDecay],
)
cgnet.architecture()
cgnet.train(x_train, y_train, x_test, y_test, epochs=100)
Related
I am using transfer learning to change my inner regression model with a outer model for classification.
The inner model is trained on the MNIST dataset where it had to predict ones from zeros. (Note that this is just an example to test the method before changing it to a more complex models and data)
I then freeze the inner model to prevent the regression output of the inner model from being changed while learning outer model.
The outer model classifies which instance the input is. So the first image of my batch should become the first class and with Tensorflow's Keras this works fine.
Then I remove the outer model's softmax activation function and then apply Grad-CAM.
Grad-CAM is applied on the final layer of the inner model (this is a 2D Conv layer).
But the grads = tape.gradient(class_channel, last_conv_layer_output) line in the code does produce only NONE.
Any help would be appreciated (I hope enough code is provided to reproduce the error ;-))
I have tried the steps from Grad-CAM Transfer learning error: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor but to no avail. In addition allowing the inner model to be retrained also produces the same problem. I am aware of the import problems with Keras and TF with regards with tape.gradient.
Libaries
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
Preprocess dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = x_train / 255
x_test = x_test / 255
# Select only ones and zeros.
x_train_zeros = x_train[y_train == 0]
x_train_ones = x_train[y_train == 1]
x_test_zeros = x_test[y_test == 0]
x_test_ones = x_test[y_test == 1]
# X and Y need to have same length
y_train_ones = x_train_ones[:len(x_train_zeros)]
y_test_ones = x_test_ones[:len(x_test_zeros)]
Model: "inner"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 28, 28, 1)] 0 []
conv2d (Conv2D) (None, 28, 28, 32) 320 ['input_1[0][0]']
add (Add) (None, 28, 28, 32) 0 ['input_1[0][0]',
'conv2d[0][0]']
conv2d_1 (Conv2D) (None, 28, 28, 32) 9248 ['add[0][0]']
add_1 (Add) (None, 28, 28, 32) 0 ['add[0][0]',
'conv2d_1[0][0]']
conv2d_2 (Conv2D) (None, 28, 28, 32) 9248 ['add_1[0][0]']
add_2 (Add) (None, 28, 28, 32) 0 ['add_1[0][0]',
'conv2d_2[0][0]']
conv2d_3 (Conv2D) (None, 28, 28, 32) 9248 ['add_2[0][0]']
add_3 (Add) (None, 28, 28, 32) 0 ['add_2[0][0]',
'conv2d_3[0][0]']
inner_final (Conv2D) (None, 28, 28, 1) 289 ['add_3[0][0]']
==================================================================================================
Total params: 28,353
Trainable params: 28,353
Non-trainable params: 0
__________________________________________________________________________________________________
history = inner_model.fit(x_train_zeros, y_train_ones, batch_size = batch_size, epochs = epochs, validation_data = (x_test_zeros, y_test_ones))
One hot encode the output for the classification
batch_size_classify = 40 # I want to "classify" the first 40 images.
X_train_class = x_train_zeros[: batch_size_classify]
# Produce "class labels"
Y_test = np.arange(0, batch_size_classify, 1, dtype=int)
from numpy import array
from numpy import argmax
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
# define example
values = array(Y_test)
# integer encode
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(values)
# binary encode
onehot_encoder = OneHotEncoder(sparse=False)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
# invert first example
#inverted = label_encoder.inverse_transform([argmax(onehot_encoded[0, :])])
X_train_class = tf.convert_to_tensor(X_train_class)
onehot_encoded = tf.convert_to_tensor(onehot_encoded)
Build the outer model
# prevent changing the inner model
inner_model.trainable = False
inp = tf.keras.layers.Input(shape=(28, 28, 1))
x = inner_model(inp)
x = tf.keras.layers.Flatten()(x)
outputs = tf.keras.layers.Dense(batch_size_2, activation = 'softmax')(x)
Outer_model = tf.keras.Model(inp, outputs)
Outer_model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
Model: "Outer"
__________________________________________________________________________________________________
Layer (type) Output Shape Param #
==================================================================================================
input_1 (InputLayer) [(None, 28, 28, 1)] 0
inner (Functional) (None, 28, 28, 1) 28353
|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
| input_1 (InputLayer) [(None, 28, 28, 1)] 0 |
| |
| conv2d (Conv2D) (None, 28, 28, 32) 320 |
| |
| add (Add) (None, 28, 28, 32) 0 |
| |
| conv2d_1 (Conv2D) (None, 28, 28, 32) 9248 |
| |
| add_1 (Add) (None, 28, 28, 32) 0 |
| |
| conv2d_2 (Conv2D) (None, 28, 28, 32) 9248 |
| |
| add_2 (Add) (None, 28, 28, 32) 0 |
| |
| conv2d_3 (Conv2D) (None, 28, 28, 32) 9248 |
| |
| add_3 (Add) (None, 28, 28, 32) 0 |
| |
| inner_final (Conv2D) (None, 28, 28, 1) 289 |
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
flatten_2 (Flatten) (None, 784) 0
dense_2 (Dense) (None, 40) 31400
==================================================================================================
Total params: 59,753
Trainable params: 31,400
Non-trainable params: 28,353
__________________________________________________________________________________________________
epochs = 20
# Fit the model to the training data.
Outer_model.fit(X_train_class, onehot_encoded, batch_size = 1, epochs = epochs)
Grad-CAM where the grads = tape.gradient(class_channel, last_conv_layer_output) line produces NONE.
def make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=None):
# First, we create a model that maps the input image to the activations of the last conv layer as well as the output predictions
grad_model = tf.keras.models.Model([model.inputs], [model.get_layer(last_conv_layer_name).output, model.output])
# Then, we compute the gradient of the top predicted class for our input
# image with respect to the activations of the last conv layer
with tf.GradientTape() as tape:
last_conv_layer_output, preds = grad_model(img_array)
if pred_index is None:
pred_index = tf.argmax(preds[0])
class_channel = preds[:, pred_index]
# This is the gradient of the output neuron (top predicted or chosen)
# with regard to the output feature map of the last conv layer
# The line in Grad-CAM where the code produces NONE...
grads = tape.gradient(class_channel, last_conv_layer_output)
# This is a vector where each entry is the mean intensity
#of the gradient over a specific feature map channel
pooled_grads = tf.reduce_mean(grads, axis = (0, 1, 2))
# We multiply each channel in the feature map array
# by "how important this channel is" with regard to the top predicted class
# then sum all the channels to obtain the heatmap class activation
last_conv_layer_output = last_conv_layer_output[0]
heatmap = last_conv_layer_output # pooled_grads[..., tf.newaxis]
heatmap = tf.squeeze(heatmap)
# For visualization purpose, we will also normalize the heatmap between 0 & 1
heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)
return heatmap.numpy()
last_conv_layer_name = "inner"
# select first image to test Grad-CAM onto.
img_array = X_train_classs[:1]
# Remove softmax activation function.
Outer_model.layers[-1].activation = None
img_array = tf.expand_dims(img_array, axis = 3)
heatmap = make_gradcam_heatmap(img_array, Outer_model, last_conv_layer_name)
Cell output:
ValueError Traceback (most recent call last)
<ipython-input-44-4dea4cd32c74> in <module>
7 img_array = tf.expand_dims(img_array, axis = 3)
8
----> 9 heatmap = make_gradcam_heatmap(img_array, U_model, last_conv_layer_name)
10
11 # Display heatmap
2 frames
<ipython-input-19-f9b93747c0ae> in make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index)
17
18 # This is a vector where each entry is the mean intensity of the gradient over a specific feature map channel
---> 19 pooled_grads = tf.reduce_mean(grads, axis = (0, 1, 2))
20
21 # We multiply each channel in the feature map array
/usr/local/lib/python3.8/dist-packages/tensorflow/python/util/traceback_utils.py in error_handler(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153 raise e.with_traceback(filtered_tb) from None
154 finally:
155 del filtered_tb
/usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
100 dtype = dtypes.as_dtype(dtype).as_datatype_enum
101 ctx.ensure_initialized()
--> 102 return ops.EagerTensor(value, ctx.device_name, dtype)
103
104
ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.
I am trying to use Transfer Learning using ResNet-50 in TensorFlow2 and Keras on CIFAR-10 dataset which has (32, 32, 3) images.
The default ResNet-50's first conv layer uses a filter size of (7, 7) with stride = 2, the resulting CIFAR-10 is reduced too much spatially here which is to be avoided. As a 'hack', the images are attempted to be upscaled from (32, 32) to (224, 224). The code is:
import tensorflow.keras as K
# Define KerasTensor as input-
input_t = K.Input(shape = (32, 32, 3))
res_model = K.applications.ResNet50(
include_top = False,
weights = "imagenet",
input_tensor = input_t
)
# Since CIFAR-10 dataset is small as compared to ImageNet, the images are upscaled to (224, 224)-
to_res = (224, 224)
model = K.models.Sequential()
model.add(K.layers.Lambda(lambda image: tf.image.resize(image, to_res)))
model.add(res_model)
model.add(K.layers.Flatten())
model.add(K.layers.BatchNormalization())
model.add(K.layers.Dense(units = 10, activation = 'softmax'))
# Choose an optimizer and loss function for training-
loss_fn = tf.keras.losses.CategoricalCrossentropy()
optimizer = tf.keras.optimizers.SGD(learning_rate = 0.1, momentum = 0.9)
model.compile(
# loss = 'categorical_crossentropy',
loss = loss_fn,
# optimizer = K.optimizers.RMSprop(lr=2e-5),
optimizer = optimizer,
metrics=['accuracy']
)
history = model.fit(
x = X_train, y = y_train,
batch_size = batch_size, epochs = 10,
validation_data = (X_test, y_test),
# callbacks=[check_point]
)
To which I get the error:
Epoch 1/10 WARNING:tensorflow:Model was constructed with shape (None,
32, 32, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 32,
32, 3), dtype=tf.float32, name='input_1'), name='input_1',
description="created by layer 'input_1'"), but it was called on an
input with incompatible shape (None, 224, 224, 3).
ValueError Traceback (most recent call
last)
in ()
2 x = X_train, y = y_train,
3 batch_size = batch_size, epochs = 10,
----> 4 validation_data = (X_test, y_test),
5 # callbacks=[check_point]
6 )
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py
in wrapper(*args, **kwargs)
975 except Exception as e: # pylint:disable=broad-except
976 if hasattr(e, "ag_error_metadata"):
--> 977 raise e.ag_error_metadata.to_exception(e)
978 else:
979 raise
ValueError: in user code:
ValueError: Input 0 is incompatible with layer resnet50: expected
shape=(None, 32, 32, 3), found shape=(None, 224, 224, 3)
The input of the model is still (32, 32, 3)
input_t = K.Input(shape = (32, 32, 3))
Using pretraining GloveVector from stanford to get the meaningful representation of each word but i want representations for a sentence containing 5-15 words, so that i can make use of cosine similarity to do a match when i receive a new sentence. I am setting a 15 words (fixed size) of each sentence and applied embedding layer then the new input shape is going to be 15 X 300 dimensions (If i have less than 15 words then padded values to make it 15 words (one random uniform distribution of 300D vector)
Below are my network shapes
[None, 15] -- Raw inputs embedding and padded(1) ID's
[None, 15, 300, 1], --input
[None, 8, 150, 128], -- conv 1
[None, 4, 75, 64], -- conv 2
[None, 2, 38, 32], -- conv 3
[None, 1, 19, 16], -- conv 4
[None, 1, 10, 4] -- conv 5
[None, 50] ---------Latent shape (new meaningful representati)------
[None, 1, 10, 4] -- encoded input for de-conv
[None, 1, 19, 16], -- conv_trans 5
[None, 2, 38, 32], -- conv_trans 4
[None, 4, 75, 64], -- conv_trans 3
[None, 8, 150, 128], -- conv_trans 2
[None, 15, 300, 1] -- conv_trans 1 -- for loss funtion with input
I have tried the CNN model with embedding layer in tensorflow
self._inputs = tf.placeholder(dtype=tf.int64, shape=[None, self.sent_len], name='input_x') #(?,15)
losses = []
# lookup layer
with tf.variable_scope('embedding') as scope:
self._W_emb = _variable_on_cpu(name='embedding', shape=[self.vocab_size, self.emb_size], initializer=tf.random_uniform_initializer(minval=-1.0, maxval=1.0))
# assigned pretrained embedding here, so initializer would be overrided
sent_batch = tf.nn.embedding_lookup(params=self._W_emb, ids=self._inputs)
sent_batch = tf.expand_dims(sent_batch, -1)
self._x = sent_batch
encoder = []
shapes = []
current_input = sent_batch
shapes.append(current_input.get_shape().as_list())
for layer_i, n_output in enumerate(n_filters[1:]):
with tf.variable_scope('Encode_conv-%d' % layer_i) as scope:
n_input = current_input.get_shape().as_list()[3]
W, wd = _variable_with_weight_decay('W-%d' % layer_i, shape=[filter_size,filter_size,n_input,n_output],
initializer=tf.random_uniform_initializer(minval=-1.0, maxval=1.0), wd=self.l2_reg)
losses.append(wd)
biases = _variable_on_cpu('bias-%d' % layer_i, shape=[n_output], initializer=tf.constant_initializer(0.00))
encoder.append(W)
output = tf.nn.relu(tf.add(tf.nn.conv2d(current_input, W, strides=[1, 2, 2, 1], padding='SAME'), biases), name=scope.name)
current_input = output
shapes.append(output.get_shape().as_list())
#z = current_input
original_shape = current_input.get_shape().as_list()
flatsize = original_shape[1]*original_shape[2]*original_shape[3]
height,width,channel = original_shape[1]*1,original_shape[2]*1,original_shape[3]*1
current_input = tf.reshape(current_input,[-1,flatsize])
with tf.variable_scope('Encode_Z-%d' % layer_i) as scope:
W_en, wd_en = _variable_with_weight_decay('W', shape=[current_input.get_shape().as_list()[1], outsize],
initializer=tf.truncated_normal_initializer(stddev=0.05),
wd=self.l2_reg)
losses.append(wd_en)
biases_en = _variable_on_cpu('bias', shape=[outsize],initializer=tf.constant_initializer(0.00))
self._z = tf.nn.relu(tf.nn.bias_add(tf.matmul(current_input, W_en), biases_en)) # Compressed representation (?,50)
with tf.variable_scope('Decode_Z-%d' % layer_i) as scope:
W_dc, wd_dc = _variable_with_weight_decay('W', shape=[self._z.get_shape().as_list()[1], current_input.get_shape().as_list()[1]],
initializer=tf.truncated_normal_initializer(stddev=0.05), wd=self.l2_reg)
losses.append(wd_dc)
biases_dc = _variable_on_cpu('bias', shape=[current_input.get_shape().as_list()[1]],initializer=tf.constant_initializer(0.00))
current_input = tf.nn.relu(tf.nn.bias_add(tf.matmul(self._z, W_dc), biases_dc))
current_input = tf.reshape(current_input,[-1,height,width,channel])
encoder.reverse()
shapes.reverse()
for layer_i, shape in enumerate(shapes[1:]):
with tf.variable_scope('Decode_conv-%d' % layer_i) as scope:
W = encoder[layer_i]
b = _variable_on_cpu('bias-%d' % layer_i, shape=[W.get_shape().as_list()[2]], initializer=tf.constant_initializer(0.00))
hh,ww,cc = shape[1], shape[2], shape[3]
output = tf.nn.relu(tf.add( tf.nn.conv2d_transpose(current_input, W, [tf.shape(sent_batch)[0],hh,ww,cc],strides=[1, 2, 2, 1],padding='SAME'), b),name=scope.name)
current_input = output
self._y = current_input
# loss
with tf.variable_scope('loss') as scope:
cross_entropy_loss = tf.reduce_mean(tf.square(current_input - sent_batch))
losses.append(cross_entropy_loss)
self._total_loss = tf.add_n(losses, name='total_loss')
opt = tf.train.AdamOptimizer(0.0001)
grads = opt.compute_gradients(self._total_loss)
self._train_op = opt.apply_gradients(grads)
But the results are not performing well because below two sentence cosine similarity is 0.9895 after getting the latent compressed representation from above model.
Functional disorders of polymorphonuclear neutrophils'
Unspecified fracture of skull, sequela'
And if i take sentences with 2-5 words and the similarity is going up to 0.9999 (suspecting the issue was caused by more default padding values with same uniform distribution from embedding lookups)
Below information may be helpful,
Total of 10,000 training samples with 10 epochs
Used Relu activations
MSE loss function
Adam optimizers
Below is the words distributions of over all sentence [
And finally can anyone suggest what's going wrong? and approach itself is not good to proceed?
I am having an Indexing Error when running the following code:
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import tflearn.datasets.mnist as mnist
X, Y, test_x, test_y = mnist.load_data(one_hot=True)
X = X.reshape([-1, 28, 28, 1])
test_x = X.reshape([-1, 28, 28, 1])
convnet = input_data(shape=[None, 28, 28, 1], name='input')
convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet,2)
convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet,2)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 10, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=0.01,
loss='categorical_crossentropy', name='targets')
model = tflearn.DNN(convnet)
model.fit({'input':X},{'targets':Y}, n_epoch=10,
validation_set=({'input':test_x},{'targets':test_y}),
snapshot_step=500, show_metric=True, run_id='mnist')
model.save('tflearncnn.model')
I cannot figure out how to make the index larger than 0-9999 (10000) as i am not sure where the error is occurring.
here is the error in my Terminal:
---------------------------------
Run id: mnist
Log directory: /tmp/tflearn_logs/
---------------------------------
Training samples: 55000
Validation samples: 55000
--
Exception in thread Thread-5:oss: 0.13790 | time: 29.813s
Traceback (most recent call last):0 - acc: 0.9592 -- iter: 31936/55000
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/home/usr/.local/lib/python3.6/site-packages/tflearn/data_flow.py", line 187, in fill_feed_dict_queue
data = self.retrieve_data(batch_ids)
File "/home/usr/.local/lib/python3.6/site-packages/tflearn/data_flow.py", line 222, in retrieve_data
utils.slice_array(self.feed_dict[key], batch_ids)
File "/home/usr/.local/lib/python3.6/site-packages/tflearn/utils.py", line 187, in slice_array
return X[start]
IndexError: index 10000 is out of bounds for axis 0 with size 10000
this happens when i reach the point where a new Epoch is supposed to start as shown when step 499 is reached:
---------------------------------
Run id: mnist
Log directory: /tmp/tflearn_logs/
---------------------------------
Training samples: 55000
Validation samples: 55000
--
Training Step: 499 | total loss: 0.12698 | time: 27.880s
| Adam | epoch: 001 | loss: 0.12698 - acc: 0.9616 -- iter: 31936/55000
I have tried the following:
-Changing size of snapshot_steps
-changing the size of n_units in fully_connected()
-changing the nb_filter in conv_2d
This is just your typo
test_x = X.reshape([-1, 28, 28, 1])
test_x = test_x.reshape([-1, 28, 28, 1])
I'm a bit new to Keras and deep learning. I'm currently trying to replicate this paper but when I'm compiling the first model (without the LSTMs) I get the following error:
"ValueError: Error when checking target: expected dense_3 to have shape (None, 120, 40) but got array with shape (8, 40, 1)"
The description of the model is this:
Input (length T is appliance specific window size)
Parallel 1D convolution with filter size 3, 5, and 7
respectively, stride=1, number of filters=32,
activation type=linear, border mode=same
Merge layer which concatenates the output of
parallel 1D convolutions
Dense layer, output_dim=128, activation type=ReLU
Dense layer, output_dim=128, activation type=ReLU
Dense layer, output_dim=T , activation type=linear
My code is this:
from keras import layers, Input
from keras.models import Model
# the window sizes (seq_length?) are 40, 1075, 465, 72 and 1246 for the kettle, dish washer,
# fridge, microwave, oven and washing machine, respectively.
def ae_net(T):
input_layer = Input(shape= (T,))
branch_a = layers.Conv1D(32, 3, activation= 'linear', padding='same', strides=1)(input_layer)
branch_b = layers.Conv1D(32, 5, activation= 'linear', padding='same', strides=1)(input_layer)
branch_c = layers.Conv1D(32, 7, activation= 'linear', padding='same', strides=1)(input_layer)
merge_layer = layers.concatenate([branch_a, branch_b, branch_c], axis=1)
dense_1 = layers.Dense(128, activation='relu')(merge_layer)
dense_2 =layers.Dense(128, activation='relu')(dense_1)
output_dense = layers.Dense(T, activation='linear')(dense_2)
model = Model(input_layer, output_dense)
return model
model = ae_net(40)
model.compile(loss= 'mean_absolute_error', optimizer='rmsprop')
model.fit(X, y, batch_size= 8)
where X and y are numpy arrays of 8 sequences of a length of 40 values. So X.shape and y.shape are (8, 40, 1). It's actually one batch of data. The thing is I cannot understand how the output would be of shape (None, 120, 40) and what these sizes would mean.
As you noted, your shapes contain batch_size, length and channels: (8,40,1)
Your three convolutions are, each one, creating a tensor like (8,40,32).
Your concatenation in the axis=1 creates a tensor like (8,120,32), where 120 = 3*40.
Now, the dense layers only work on the last dimension (the channels in this case), leaving the length (now 120) untouched.
Solution
Now, it seems you do want to keep the length at the end. So you won't need any flatten or reshape layers. But you will need to keep the length 40, though.
You're probably doing the concatenation in the wrong axis. Instead of the length axis (1), you should concatenate in the channels axis (2 or -1).
So, this should be your concatenate layer:
merge_layer = layers.Concatenate()([branch_a, branch_b, branch_c])
#or layers.Concatenate(axis=-1)([branch_a, branch_b, branch_c])
This will output (8, 40, 96), and the dense layers will transform the 96 in something else.