I wanted to use CNN for the classification of my dataset which is numerical dataset. My dataset is 3200x36 size. Whenever I used the following code and passed my data, I did not get any result. For the accuracy, it just runs but do not output anything. What did I do wrong, Please explain.
x_train, x_test, y_train, y_test = train_test_split(feature, target, train_size=0.75)
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model = Sequential()
model.add(Conv2D(filters=64, kernel_size=3, activation='relu'))
model.add(MaxPooling2D(pool_size=4))
model.add(Dense(3, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=100, batch_size=8149, verbose=1)
accuracy = model.evaluate(x_test, y_test)
print( model.evaluate(x_test, y_test))
Related
I am learning about neural networks with Kaggle tutorials. I have made a neural net to predict concrete strength and I want to display the MSE (for starters) metric after fitting the model. I have failed both with print(metrics) and plotting the metrics (displays an empty graph).
df = concrete.copy()
df_train = df.sample(frac=0.7, random_state=0)
df_valid = df.drop(df_train.index)
X_train = df_train.drop('CompressiveStrength', axis=1)
X_valid = df_valid.drop('CompressiveStrength', axis=1)
y_train = df_train['CompressiveStrength']
y_valid = df_valid['CompressiveStrength']
model = keras.Sequential([
layers.BatchNormalization(),
layers.Dense(512, activation='relu', input_shape=input_shape),
layers.BatchNormalization(),
layers.Dense(512, activation='relu'),
layers.Dropout(rate=0.3), # apply 30% dropout to the next layer
layers.Dense(512, activation='relu'),
layers.BatchNormalization(),
layers.Dense(512, activation='relu'),
layers.BatchNormalization(),
layers.Dense(1),
])
model.compile(
optimizer='sgd', # SGD is more sensitive to differences of scale
loss='mse',
metrics=[tf.keras.metrics.MeanSquaredError()]
)
history = model.fit(
X_train, y_train,
validation_data=(X_valid, y_valid),
batch_size=64,
epochs=100,
verbose=0,
callbacks=[early_stopping],
)
print(history)
pyplot.plot(history.history['mean_squared_error'])
I'm using keras to perform multilabel classification. I'm using 'binary_crossentropy' as loss function, metrics=['accuracy'], 'sigmoid' as activation.
During the training I saw accuracy above the 90%, and also using evaluate on the test set I have something similar.
If I try to manually compute the accuracy using predict module the accuracy also on the training set dramatically leaves 45%.
This is the model:
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(8, kernel_size=3, strides=2, activation='relu', input_shape=(N_qubits, N_qubits,2)))
model.add(keras.layers.Conv2D(16, kernel_size=2, activation='relu'))
model.add(keras.layers.Dense(64, activation='relu'))
model.add(keras.layers.Dense(128, activation='relu'))
model.add(keras.layers.Conv2D(32, kernel_size=1, activation='relu'))
model.add(keras.layers.GlobalMaxPooling2D())
model.add(keras.layers.Dense(units=y_train.shape[1], activation='sigmoid'))
adam_optimizer = keras.optimizers.adam()
model.compile(loss='binary_crossentropy', optimizer=adam_optimizer, metrics=['accuracy'])
history = model.fit(X_train, y_train, batch_size=150, epochs=1000, verbose=1, validation_split=0.05)
Here where I use evaluate()
results = model.evaluate(X_test, y_test, batch_size=128)
Here when I use predict()
y_pred_train = model.predict(X_train)
y_pred_test = model.predict(X_test)
Iam using 1dCNN for time series data but following error occur in model.fit line.
The error is as follows :
Error when checking input: expected conv1d_11_input to have shape (6700, 1)
but got array with shape (1, 1).
any one plz help
code portion is as below
dataframe = pd.read_excel("file path", header=None,delim_whitespace=True)
dataset = dataframe.values
X=dataframe.values[:,0]
Y=dataframe.values[:,2]
X = np.expand_dims(X, axis=1)
Y = np.expand_dims(Y, axis=1)
(X_train, X_test, Y_train, Y_test) = train_test_split(X, Y, test_size=0.33, random_state=seed)
X_train = np.reshape(X_train, (-1, X_train.shape[1],1))
Y_train = np.reshape(Y_train, (Y_train.shape[0], 1, Y_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
print(X_train.shape)
print(Y_train.shape)
n_timesteps, n_features, n_outputs = X_train.shape[0], X_train.shape[1], Y_train.shape[1]
verbose, epochs, batch_size = 0, 100, 32
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,1)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
model.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size, verbose=verbose)'
Use:
n_timesteps, n_outputs = X_train.shape[1], Y_train.shape[1]
dataset = data.values
dataset[:, [0,2,3,4,5,6,7,8,9]]
Y = dataset[:, 1]
X_train, X_test, y_train, y_test = train_test_split(X, Y, stratify=Y, random_state=0)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit(X_train).transform(X_train)
X_test_scaled = scaler.fit(X_test).transform(X_test)
model = Sequential()
model.add(Dense(50, input_dim = 9, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='mean_squared_error', optimizer='Adam', metrics=['accuracy'])
model.summary()
model.fit(X_train_scaled, y_train, epochs=50)
print('\n accuracy : {:.4f}'.format(model.evaluate(X_test_scaled, y_test)[1]))
enter image description here
i think the problem is Scaler.. but i don't know what i do...
why the loss function has nan output, and why the accuracy is 0?
Recently I just started learning to implement neural network using Keras, and I tried to implement Le-Net5 NN for MNIST problem based on the structure listed as below.
Code:
# Load the data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Parameter set up
input_shape = (32,32,1)
batch_size = 128
# Format the image info
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
x_train = np.pad(x_train, ((0,0),(2,2),(2,2),(0,0)), 'constant')
x_test = np.pad(x_test, ((0,0),(2,2),(2,2),(0,0)), 'constant')
# Encode label
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# Construct the model
model = keras.Sequential()
conv_stride = 1
pooling_stride = 2
model.add(layers.Conv2D(filters=6, kernel_size=[5,5], input_shape=input_shape, padding="valid", strides=[conv_stride,conv_stride], activation='tanh'))
model.add(layers.AveragePooling2D(pool_size=[2,2], padding="valid", strides=pooling_stride))
model.add(layers.Conv2D(filters=16, kernel_size=[5,5], padding="valid", strides=[conv_stride,conv_stride],activation='tanh'))
model.add(layers.AveragePooling2D(pool_size=[2,2], padding="valid", strides=pooling_stride))
model.add(layers.Conv2D(filters=120, kernel_size=[5,5], padding="valid", strides=[conv_stride,conv_stride], activation='tanh'))
model.add(layers.Flatten())
model.add(layers.Dense(84, activation='tanh'))
model.add(layers.Dense(10, activation=tf.nn.softmax))
print(model.summary())
model.compile(optimizer="sgd", loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=batch_size, verbose=1, epochs=12)
score = model.evaluate(x_test, y_test, verbose=1)
print("Test Accuracy: ", score[1])
However, there's an error when I run the program, the error message is:
Matrix size-incompatible: In[0]: [128,1536], In[1]: [1176,200]
[[{{node dense/MatMul}} = MatMul[T=DT_FLOAT, _class=["loc:#training/Adam/gradients/dense/MatMul_grad/MatMul"], transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](flatten/Reshape, dense/MatMul/ReadVariableOp)]]
I have checked the structure using model.summary() and it seems like the neural network's structure is correct. Can anybody tell me which part leads to the error?