Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a dataset of images I want to input to a Convolutional Neural Network Model, however, with each of these images, there is a range or distance from the object associated with the image.
I want to input this range as an additional piece of context for the CNN model.
Does providing this extra piece of information provide any benefit? Does it make sense to do? Is it feasible in Keras?
Thanks!
You have a few options here, one is to encode your numerical value as a feature plane in your input. If your numerical value is c you can add a channel to each input image with the value c at every pixel.
Another option is to merge the value in as an additional input to the fully connected layer.
In keras this would look something like:
conv = Sequential()
conv.add(Conv2D(32, kernel_size=(3, 3), strides=(1, 1),
activation='relu',
input_shape=input_shape))
conv.add(MaxPooling2D(pool_size=(2, 2)))
conv.add(Flatten())
conv.add(Dense(512, activation='relu'))
range_ = Sequential()
range_.add(Dense(1, input_shape=(1,), activation='relu'))
merged = Concatenate([conv, range_])
merged.add(Dense(n_classes, activation='softmax'))
merged.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
Which option you choose will depend on your data and whether you think the numerical feature will help the convolutional layers better understand the input, or whether you think it's not needed until later. If you have time you can try both architectures and see which performs better.
Related
I am trying to have two sequential models with one input and one output, I am not sure if this is possible as I've been researching Keras etc. and all that I seem to be investigating is how to do two and three inputs with multiple outputs which is what I DO NOT WANT.
To explain, from the 1st sequential model level output AT THE FLATTEN LAYER, I am trying to convert the activations back to a cube shape to be passed as input to the 2nd level sequential model making this a 2 level multi-level structure. I have my diagram to assist with the understanding of my thoughts, please review such in the link.
My apologies in advance for the lack of understanding and explanations, I am not sure of the terminology for such a model or approach hence my diagram to clarify my thoughts at this stage. All I came up with is a multi-level sequential model and its fusuion mechanisms. I searched for examples of such but thus far my efforts were unsuccessful.
THANK YOU'ssssss in advance for any assistance given.
enter image description here
I was following this code but it leads to a the fusion of two inputs. I would like to use the last flattened layer of the 1st level as input for the 2nd level, then process onwards towards classification. I am not sure if this is possible please assist?
model = Sequential()
model.add(Conv3D(32, kernel_size=(3, 3, 3),activation='relu',
input_shape=input_shape))
model.add(Conv3D(64, (3, 3, 3), activation='relu'))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
print(model.output_shape)
# The additional data (the coordinates x,y,z)
extra = Sequential()
extra.add(Activation('sigmoid', input_shape=(3,)))
print(extra.output_shape)
merged = Concatenate([model, extra])
# New model should encompass the outputs of the convolutional
network and the coordinates that have been merged.
# But how?
new_model = Sequential()
new_model.add(Dense(128, activation='relu'))
new_model.add(Dropout(0.8))
new_model.add(Dense(32, activation='sigmoid'))
new_model.add(Dense(num_classes, activation='softmax'))
new_model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Since my data are not normally distributed so I decided to use PowerTransformer on X, y before splitting them to X_train, X_test, y_train, y_test. Is it okay if I do this or I should perform transformation later. Here is my code:
X = df[['Aces', 'TotalPointsWon', 'ServiceGamesWon', 'TotalServicePointsWon']]
y = df[['Winnings']]
transformer_X = PowerTransformer()
X_log = transformer_X.fit_transform(X)
transformer_y = PowerTransformer()
y_log = transformer_y.fit_transform(y)
X_train, X_test, y_train, y_test = train_test_split(X_log, y_log, train_size=0.8)
scaler = StandardScaler()
scaler.fit_transform(X_train)
scaler.transform(X_test)
model = LinearRegression()
model.fit(X_train, y_train)
Residuals Analyses Graph
Thanks for helping out.
PowerTransformer makes data more Gaussian-like, feature-wise.
Just like any data preprocessing step, the rule of thumb is to fit (i.e learn the parameters) the training data, then transform both the latter and the test set (i.e applying the learned parameters to the unseen new data).
Hence, the fit method should only be applied to the training data, with the assumption that it represents the statistical distribution of whole sample (i.e. make sure to use stratified splits if it is a classification problem, and make sure you have enough examples, use cross validation, ...etc).
Why?
Because at some time, you'll receive new unseen data that you'll have only to transform. That's why you're splitting the data at the stage, to simulate this event and validate that the model is not overfitting nor underfitting and did actually learn how to represent the data.
Otherwise, your model would be biased, and data snooping will be, to a certain degree, applicable here.
Final words
Please note that PowerTransformer accepts a parameter called method that specifies one of the two available power transform methods:
yeo-johnson: which works with positive and negative values.
box-cox: which only works with strictly positive values.
You can read more about them here and here, respectively.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have data with the dimension of (2055, 95). I split it into train data: (1640, 95) and validation data: (415, 95).
I build a KNN classifier but don't know which k param to choose so set k in range and find out which k is fit for my problem. But I got this data:
I know that if we choose k = 1 means that the model is overfitting. So in my case, the best k is 3?
To determine the optimal k parameter in KNN, I would suggest to plot silhouette coefficient for different k values and apply elbow method to determine which one is the most suitable.
silhouette_coefficients = []
for k in range(2, 11):
kmeans = KMeans(n_clusters=k, **kmeans_kwargs)
kmeans.fit(scaled_features)
score = silhouette_score(scaled_features, kmeans.labels_)
silhouette_coefficients.append(score)
plt.style.use("fivethirtyeight")
plt.plot(range(2, 11), silhouette_coefficients)
plt.xticks(range(2, 11))
plt.xlabel("Number of Clusters")
plt.ylabel("Silhouette Coefficient")
plt.show()
For such a case below the optimal would be 3 since the rate of change decreases after x=3.
You can have a look at https://code-ai.mk/kmeans-elbow-method-tutorial/ for further information on elbow method.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am working on an image pixel classification problem and use
data augmentation (in Keras).
So I apply data transformations (rotations, flips) to image patches. My code for data augmentation and training the CNN is given below.
datagen = ImageDataGenerator(
rotation_range=40,
horizontal_flip=True,
vertical_flip=True,
)
batch_size=16
epochs=50
# compile the model
model.compile(loss='categorical_crossentropy',
optimizer=Adam(),
metrics=['accuracy'])
model_checkpoint = ModelCheckpoint('myweights.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [plot_losses,model_checkpoint]
history=model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),
steps_per_epoch=x_train.shape[0] // batch_size,
callbacks=callbacks_list,
validation_data = datagen.flow(x_valid, y_valid, batch_size=batch_size),
validation_steps=x_valid.shape[0] // batch_size,
epochs = epochs, verbose = 1)
My train/validation accuracy and loss plots are as follows:
I can see there is a general continual increase in accuracy and drop in loss , which is what we want. But it is very slow across 20 epochs. Without data augmentation my accuracy increases faster.
So why is it that data augmentation results in such a slow learning process (approximately 48% to 58% training/valid accuracy increase over 20 epochs)?
I am using the Adam optimizer which uses exponential learning rate decay, so I do not believe a new learning rate schedule would affect much unless I am missing something. Any insights are welcome.
It is expected behavior when use data augmentation for your model to train slower. Augmentation flips, rotates and in general transforms an image to enlarge our data set. This is done with CPU which is slower than GPU.
We use augmentation not for speed but for increased accuracy.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I try to implement a neural network. I'm using backpropagation to compute the gradients. After obtaining the gradients, I multiply them by the learning rate and subtract them from the corresponding weights. (basically trying to apply gradient descent, please tell me if this is wrong).
So the first thing I tried after having the backpropagation and gradient descent ready, was to train a simple XOR classifier where the inputs can be (0,0), (1,0), (0,1), (1,1) and the corresponding outputs are 0, 1, 1, 0. So my neural network contains 2 input units, 1 output unit and one hidden layer with 3 units on it. When training it with a learning rate of 3.0 for >100 (even tried >5000), the cost drops until a specific point where it gets stuck, so it's remaining constant. The weights are randomly initialized each time I run the program, but it always gets stuck at the same specific cost. Anyways, after the training is finished I tried to run my neural network on any of the above inputs and the output is always 0.5000. I thought about changing the inputs and outputs so they are : (-1,-1), (1, -1), (-1, 1), (1, 1) and the outputs -1, 1, 1, -1. Now when trained with the same learning rate, the cost is dropping continuously, no matter the number of iterations but the results are still wrong, and they always tend to be very close to 0. I even tried to train it for an insane number of iterations and the results are the following: [ iterations: (20kk), inputs:(1, -1), output:(1.6667e-08) ] and also [iterations: (200kk), inputs:(1, -1), output:(1.6667e-09) ], also tried for inputs(1,1) and others, the output is also very close to 0. It seems like the output is always mean(min(y), max(y)), it doesn't matter in what form I provide the input/output. I can't figure out what I'm doing wrong, can someone please help?
There are so many places where you might be wrong:
check your gradients numerically
you have to use nonlinear hidden units to learn XOR - do you have non-linear activation there?
you need bias neuron, do you have one?
minor things that should not cause the mentioned problem, but worth fixing either way:
do you have sigmoidal activation in the output node (as your network is a classifier)?
do you train with cross-entropy cost (although this is minor problem)?