print("Epochs: ", epochs)
print("Batch_size: ", batch_size)
print("Training loss: ", model.loss(x_train_reshaped, y_train))
print("Test loss: ", model.loss(x_test_reshaped, y_test))
print("Accuracy: ", model.accuracy(x_train_reshaped, y_train))
print("Time spent: ", CumulativeTime(time_steps, n_timesteps))
print("ๅ็กฎ็๏ผ", model. Accuracy(x_train_reshaped, y_train))
Epochs: 50000 Batch_size: 4000 Training loss: 0.8428 Test loss: 0.9553 Accuracy: 98.47% Time spent: 6.
import numpy as np
from numpy import mean
from numpy import std
from numpy import dstack
from pandas import read_csv
from matplotlib import pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import Dropout
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
from keras.utils import to_categorical
def readucr(filename):
data = np.loadtxt(filename, delimiter=",")
y = data[:, 0]
x = data[:, 1:]
return x, y.astype(int)
x_train, y_train = readucr("EDXRF_HC_TRAINING.csv")
x_test, y_test = readucr("EDXRF_HC_TESTING.csv")
#Converting kernals
sample_size = x_train.shape[0] # Nn of samples in the train set
time_steps = x_train.shape[1]# Number of features in the train set
input_dimension = 1 # Each number represented by 1
x_train_reshaped = x_train.reshape(sample_size, time_steps, input_dimension)
print("After reshape train data set shape:\n",x_train_reshaped.shape)
print("1 sample shape:\n",x_train_reshaped[0].shape)
print ("an example sample:\n", x_train_reshaped[0])
sample_size = x_test.shape[0] # Nn of samples in the train set # x_train is the input name
time_steps = x_test.shape[1]# Number of features in the train set
input_dimension = 1 # Each number represented by 1
x_test_reshaped = x_test.reshape(sample_size, time_steps, input_dimension)
print("After reshape train data set shape:\n",x_test_reshaped.shape)
print("1 sample shape:\n",x_test_reshaped[0].shape)
print ("an example sample:\n", x_test_reshaped[0])
# def model
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(1023, 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(1, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit and evaluate a model
def evaluate_model(x_train_reshaped, y_train, x_test_reshaped, y_test):
verbose, epochs, batch_size = 0, 500, 4000
n_timesteps, n_features, n_outputs = x_train_reshaped.shape[1], x_train_reshaped.shape[2], y_train.shape[1]
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,n_features)))
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'])
# fit network
# evaluate model
history = model.fit(x_train_reshaped, y_train, epochs=500, validation_split=0.2, verbose=1 )
model.summary()