code stringlengths 17 6.64M |
|---|
class DataSet():
def __init__(self, X, y, nb_classes, scaling=True, test_size=0.2, random_state=0):
'\n X is originally vector. Hence, it will be transformed\n to 2D images with a channel (i.e, 3D).\n '
self.X = X
self.add_channels()
X = self.X
(X_trai... |
class Machine():
def __init__(self, X, y, nb_classes=2, fig=True):
self.nb_classes = nb_classes
self.set_data(X, y)
self.set_model()
self.fig = fig
def set_data(self, X, y):
nb_classes = self.nb_classes
self.data = DataSet(X, y, nb_classes)
print('data... |
class Machine_Generator(aicnn.Machine):
def __init__(self, X, y, nb_classes=2, steps_per_epoch=10, fig=True, gen_param_dict=None):
super().__init__(X, y, nb_classes=nb_classes, fig=fig)
self.set_generator(steps_per_epoch=steps_per_epoch, gen_param_dict=gen_param_dict)
def set_generator(self,... |
class CNN(aicnn.CNN):
def __init__(model, input_shape, nb_classes, n_dense=128, p_dropout=0.5, BN_flag=False, PretrainedModel=VGG16):
'\n If BN_flag is True, BN is used instaed of Dropout\n '
model.in_shape = input_shape
model.n_dense = n_dense
model.p_dropout = p_dr... |
class DataSet(aicnn.DataSet):
def __init__(self, X, y, nb_classes, n_channels=3, scaling=True, test_size=0.2, random_state=0):
self.n_channels = n_channels
super().__init__(X, y, nb_classes, scaling=scaling, test_size=test_size, random_state=random_state)
def add_channels(self):
n_ch... |
class Machine_Generator(aigen.Machine_Generator):
'\n This Machine Generator is for pretrained approach.\n '
def __init__(self, X, y, nb_classes=2, steps_per_epoch=10, n_dense=128, p_dropout=0.5, BN_flag=False, scaling=False, PretrainedModel=VGG16, fig=True, gen_param_dict=None):
'\n sca... |
def coeff_determination(y_true, y_pred):
SS_res = K.sum(K.square((y_true - y_pred)))
SS_tot = K.sum(K.square((y_true - K.mean(y_true))))
return (1 - (SS_res / (SS_tot + K.epsilon())))
|
def unique_filename(type='uuid'):
if (type == 'datetime'):
filename = datetime.datetime.now().strftime('%y%m%d_%H%M%S')
else:
filename = str(uuid.uuid4())
return filename
|
def makenewfold(prefix='output_', type='datetime'):
suffix = unique_filename('datetime')
foldname = ('output_' + suffix)
os.makedirs(foldname)
return foldname
|
def save_history_history(fname, history_history, fold=''):
np.save(os.path.join(fold, fname), history_history)
|
def load_history_history(fname, fold=''):
history_history = np.load(os.path.join(fold, fname)).item(0)
return history_history
|
def plot_acc(history, title=None):
if (not isinstance(history, dict)):
history = history.history
plt.plot(history['acc'])
plt.plot(history['val_acc'])
if (title is not None):
plt.title(title)
plt.ylabel('Accracy')
plt.xlabel('Epoch')
plt.legend(['Training data', 'Validation... |
def plot_loss(history, title=None):
if (not isinstance(history, dict)):
history = history.history
plt.plot(history['loss'])
plt.plot(history['val_loss'])
if (title is not None):
plt.title(title)
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Training data', 'Validation... |
def plot_history(history):
plt.figure(figsize=(15, 5))
plt.subplot(1, 2, 1)
plot_acc(history)
plt.subplot(1, 2, 2)
plot_loss(history)
|
def plot_loss_acc(history):
plot_loss(history, '(a) Loss trajectory')
plt.show()
plot_acc(history, '(b) Accracy trajectory')
plt.show()
|
def plot_acc_loss(history):
plot_acc(history, '(a) Accracy trajectory')
plt.show()
plot_loss(history, '(b) Loss trajectory')
plt.show()
|
def save_history_history(fname, history_history, fold=''):
np.save(os.path.join(fold, fname), history_history)
|
def load_history_history(fname, fold=''):
history_history = np.load(os.path.join(fold, fname)).item(0)
return history_history
|
def plot_acc(history, title=None):
if (not isinstance(history, dict)):
history = history.history
plt.plot(history['acc'])
plt.plot(history['val_acc'])
if (title is not None):
plt.title(title)
plt.ylabel('μ νλ')
plt.xlabel('μν¬ν¬')
plt.legend(['νμ΅ λ°μ΄ν° μ±λ₯', 'κ²μ¦ λ°μ΄ν° μ±λ₯'], loc=0)
|
def plot_loss(history, title=None):
if (not isinstance(history, dict)):
history = history.history
plt.plot(history['loss'])
plt.plot(history['val_loss'])
if (title is not None):
plt.title(title)
plt.ylabel('μμ€')
plt.xlabel('μν¬ν¬')
plt.legend(['νμ΅ λ°μ΄ν° μ±λ₯', 'κ²μ¦ λ°μ΄ν° μ±λ₯'], loc=0... |
def plot_history(history):
plt.figure(figsize=(15, 5))
plt.subplot(1, 2, 1)
plot_acc(history)
plt.subplot(1, 2, 2)
plot_loss(history)
|
def plot_loss_acc(history):
plot_loss(history, '(a) μμ€ μΆμ΄')
plt.show()
plot_acc(history, '(b) μ νλ μΆμ΄')
plt.show()
|
def plot_acc_loss(history):
plot_acc(history, '(a) μ νλ μΆμ΄')
plt.show()
plot_loss(history, '(b) μμ€ μΆμ΄')
plt.show()
|
class ARGS():
pass
|
def kproc(x):
return (((x ** 2) + (2 * x)) + 1)
|
def kshape(input_shape):
return input_shape
|
class Mult(Layer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def call(self, x):
return (((x ** 2) + (2 * x)) + 1)
|
def kproc_concat(x):
m = K.mean(x, axis=1, keepdims=True)
d1 = K.abs((x - m))
d2 = K.square((x - m))
return K.concatenate([x, d1, d2], axis=1)
|
def kshape_concat(input_shape):
output_shape = list(input_shape)
output_shape[1] *= 3
return tuple(output_shape)
|
class DNN(models.Sequential):
def __init__(self, Nin, Nh_l, Nout):
super().__init__()
self.add(layers.Dense(Nh_l[0], activation='relu', input_shape=(Nin,), name='Hidden-1'))
self.add(layers.Dense(Nh_l[1], activation='relu', name='Hidden-2'))
self.add(layers.Dense(Nout, activation=... |
def main():
Nin = 784
Nh_l = [100, 50]
number_of_class = 10
Nout = number_of_class
((X_train, Y_train), (X_test, Y_test)) = Data_func()
model = DNN(Nin, Nh_l, Nout)
history = model.fit(X_train, y_train, epochs=10, batch_size=100, validation_split=0.2)
performace_test = model.evaluate(X... |
class DNN(models.Sequential):
def __init__(self, Nin, Nh_l, Nout):
super().__init__()
self.add(layers.Dense(Nh_l[0], activation='relu', input_shape=(Nin,), name='Hidden-1'))
self.add(layers.Dense(Nh_l[1], activation='relu', name='Hidden-2'))
self.add(layers.Dense(Nout, activation=... |
def Data_func():
((X_train, y_train), (X_test, y_test)) = datasets.cifar10.load_data()
Y_train = np_utils.to_categorical(y_train)
Y_test = np_utils.to_categorical(y_test)
(L, W, H, C) = X_train.shape
X_train = X_train.reshape((- 1), ((W * H) * C))
X_test = X_test.reshape((- 1), ((W * H) * C))
... |
def main():
Nh_l = [100, 50]
number_of_class = 10
Nout = number_of_class
((X_train, Y_train), (X_test, Y_test)) = Data_func()
model = DNN(X_train.shape[1], Nh_l, Nout)
history = model.fit(X_train, y_train, epochs=10, batch_size=100, validation_split=0.2)
performace_test = model.evaluate(X_... |
class DNN(models.Sequential):
def __init__(self, Nin, Nh_l, Nout):
super().__init__()
self.add(layers.Dense(Nh_l[0], activation='relu', input_shape=(Nin,), name='Hidden-1'))
self.add(layers.Dense(Nh_l[1], activation='relu', name='Hidden-2'))
self.add(layers.Dense(Nout, activation=... |
def Data_func():
((X_train, y_train), (X_test, y_test)) = datasets.cifar10.load_data()
Y_train = np_utils.to_categorical(y_train)
Y_test = np_utils.to_categorical(y_test)
(L, W, H, C) = X_train.shape
X_train = X_train.reshape((- 1), ((W * H) * C))
X_test = X_test.reshape((- 1), ((W * H) * C))
... |
def main():
Nh_l = [100, 50]
number_of_class = 10
Nout = number_of_class
((X_train, Y_train), (X_test, Y_test)) = Data_func()
model = DNN(X_train.shape[1], Nh_l, Nout)
history = model.fit(X_train, y_train, epochs=10, batch_size=100, validation_split=0.2)
performace_test = model.evaluate(X_... |
class DNN(models.Sequential):
def __init__(self, Nin, Nh_l, Nout):
super().__init__()
self.add(layers.Dense(Nh_l[0], activation='relu', input_shape=(Nin,), name='Hidden-1'))
self.add(layers.Dropout(0.05))
self.add(layers.Dense(Nh_l[1], activation='relu', name='Hidden-2'))
... |
def Data_func():
((X_train, y_train), (X_test, y_test)) = datasets.cifar10.load_data()
Y_train = np_utils.to_categorical(y_train)
Y_test = np_utils.to_categorical(y_test)
(L, W, H, C) = X_train.shape
X_train = X_train.reshape((- 1), ((W * H) * C))
X_test = X_test.reshape((- 1), ((W * H) * C))
... |
def main():
Nh_l = [100, 50]
number_of_class = 10
Nout = number_of_class
((X_train, Y_train), (X_test, Y_test)) = Data_func()
model = DNN(X_train.shape[1], Nh_l, Nout)
history = model.fit(X_train, y_train, epochs=10, batch_size=100, validation_split=0.2)
performace_test = model.evaluate(X_... |
class DNN(models.Sequential):
def __init__(self, Nin, Nh_l, Nout):
super().__init__()
self.add(layers.Dense(Nh_l[0], activation='relu', input_shape=(Nin,), name='Hidden-1'))
self.add(layers.Dropout(0.05))
self.add(layers.Dense(Nh_l[1], activation='relu', name='Hidden-2'))
... |
def Data_func():
((X_train, y_train), (X_test, y_test)) = datasets.cifar10.load_data()
Y_train = np_utils.to_categorical(y_train)
Y_test = np_utils.to_categorical(y_test)
(L, W, H, C) = X_train.shape
X_train = X_train.reshape((- 1), ((W * H) * C))
X_test = X_test.reshape((- 1), ((W * H) * C))
... |
def main():
Nh_l = [100, 50]
number_of_class = 10
Nout = number_of_class
((X_train, Y_train), (X_test, Y_test)) = Data_func()
model = DNN(X_train.shape[1], Nh_l, Nout)
history = model.fit(X_train, y_train, epochs=10, batch_size=100, validation_split=0.2)
performace_test = model.evaluate(X_... |
class DNN(models.Sequential):
def __init__(self, Nin, Nh_l, Nout):
super().__init__()
self.add(layers.Dense(Nh_l[0], activation='relu', input_shape=(Nin,), name='Hidden-1'))
self.add(layers.Dropout(0.05))
self.add(layers.Dense(Nh_l[1], activation='relu', name='Hidden-2'))
... |
def Data_func():
((X_train, y_train), (X_test, y_test)) = datasets.cifar10.load_data()
Y_train = np_utils.to_categorical(y_train)
Y_test = np_utils.to_categorical(y_test)
(L, W, H, C) = X_train.shape
X_train = X_train.reshape((- 1), ((W * H) * C))
X_test = X_test.reshape((- 1), ((W * H) * C))
... |
def main():
Nh_l = [100, 50]
number_of_class = 10
Nout = number_of_class
((X_train, Y_train), (X_test, Y_test)) = Data_func()
model = DNN(X_train.shape[1], Nh_l, Nout)
history = model.fit(X_train, y_train, epochs=10, batch_size=100, validation_split=0.2)
performace_test = model.evaluate(X_... |
class DNN(models.Sequential):
def __init__(self, Nin, Nh_l, Nout):
super().__init__()
self.add(layers.Dense(Nh_l[0], activation='relu', input_shape=(Nin,), name='Hidden-1'))
self.add(layers.Dropout(0.01))
self.add(layers.Dense(Nh_l[1], activation='relu', name='Hidden-2'))
... |
def Data_func():
((X_train, y_train), (X_test, y_test)) = datasets.cifar10.load_data()
Y_train = np_utils.to_categorical(y_train)
Y_test = np_utils.to_categorical(y_test)
(L, W, H, C) = X_train.shape
X_train = X_train.reshape((- 1), ((W * H) * C))
X_test = X_test.reshape((- 1), ((W * H) * C))
... |
def main():
Nh_l = [100, 50]
number_of_class = 10
Nout = number_of_class
((X_train, Y_train), (X_test, Y_test)) = Data_func()
model = DNN(X_train.shape[1], Nh_l, Nout)
history = model.fit(X_train, y_train, epochs=10, batch_size=100, validation_split=0.2)
performace_test = model.evaluate(X_... |
class DNN(models.Sequential):
def __init__(self, Nin, Nh_l, Nout):
super().__init__()
self.add(layers.Dense(Nh_l[0], activation='relu', input_shape=(Nin,), name='Hidden-1'))
self.add(layers.Dropout(0.01))
self.add(layers.Dense(Nh_l[1], activation='relu', name='Hidden-2'))
... |
def Data_func():
((X_train, y_train), (X_test, y_test)) = datasets.cifar10.load_data()
Y_train = np_utils.to_categorical(y_train)
Y_test = np_utils.to_categorical(y_test)
(L, W, H, C) = X_train.shape
X_train = X_train.reshape((- 1), ((W * H) * C))
X_test = X_test.reshape((- 1), ((W * H) * C))
... |
def main():
Nh_l = [100, 50]
number_of_class = 10
Nout = number_of_class
((X_train, Y_train), (X_test, Y_test)) = Data_func()
model = DNN(X_train.shape[1], Nh_l, Nout)
history = model.fit(X_train, y_train, epochs=10, batch_size=100, validation_split=0.2)
performace_test = model.evaluate(X_... |
class DNN(models.Sequential):
def __init__(self, Nin, Nh_l, Nout):
super().__init__()
self.add(layers.Dense(Nh_l[0], activation='relu', input_shape=(Nin,), name='Hidden-1'))
self.add(layers.Dropout(0.01))
self.add(layers.Dense(Nh_l[1], activation='relu', name='Hidden-2'))
... |
def Data_func():
((X_train, y_train), (X_test, y_test)) = datasets.cifar10.load_data()
Y_train = np_utils.to_categorical(y_train)
Y_test = np_utils.to_categorical(y_test)
(L, W, H, C) = X_train.shape
X_train = X_train.reshape((- 1), ((W * H) * C))
X_test = X_test.reshape((- 1), ((W * H) * C))
... |
def main():
Nh_l = [100, 50]
number_of_class = 10
Nout = number_of_class
((X_train, Y_train), (X_test, Y_test)) = Data_func()
model = DNN(X_train.shape[1], Nh_l, Nout)
history = model.fit(X_train, y_train, epochs=10, batch_size=100, validation_split=0.2)
performace_test = model.evaluate(X_... |
class DNN(models.Sequential):
def __init__(self, Nin, Nh_l, Pd_l, Nout):
super().__init__()
self.add(layers.Dense(Nh_l[0], activation='relu', input_shape=(Nin,), name='Hidden-1'))
self.add(layers.Dropout(Pd_l[0]))
self.add(layers.Dense(Nh_l[1], activation='relu', name='Hidden-2'))... |
def Data_func():
((X_train, y_train), (X_test, y_test)) = datasets.cifar10.load_data()
Y_train = np_utils.to_categorical(y_train)
Y_test = np_utils.to_categorical(y_test)
(L, W, H, C) = X_train.shape
X_train = X_train.reshape((- 1), ((W * H) * C))
X_test = X_test.reshape((- 1), ((W * H) * C))
... |
def main():
Nh_l = [100, 50]
Pd_l = [0.0, 0.0]
number_of_class = 10
Nout = number_of_class
((X_train, Y_train), (X_test, Y_test)) = Data_func()
model = DNN(X_train.shape[1], Nh_l, Pd_l, Nout)
history = model.fit(X_train, Y_train, epochs=100, batch_size=100, validation_split=0.2)
perfor... |
class AE(models.Model):
def __init__(self, x_nodes=784, z_dim=36):
x_shape = (x_nodes,)
x = layers.Input(shape=x_shape)
z = layers.Dense(z_dim, activation='relu')(x)
y = layers.Dense(x_nodes, activation='sigmoid')(z)
super().__init__(x, y)
self.x = x
self.z... |
def show_ae(autoencoder):
encoder = autoencoder.Encoder()
decoder = autoencoder.Decoder()
encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)
n = 10
plt.figure(figsize=(20, 6))
for i in range(n):
ax = plt.subplot(3, n, (i + 1))
plt.imshow(x_t... |
def main(epochs=20):
x_nodes = 784
z_dim = 36
((X_train, Y_train), (X_test, Y_test)) = Data_func()
autoencoder = AE(x_nodes, z_dim)
history = autoencoder.fit(x_train, x_train, epochs=epochs, batch_size=256, shuffle=True, validation_data=(x_test, x_test))
plot_acc(history)
plt.show()
pl... |
def Conv2D(filters, kernel_size, padding='same', activation='relu'):
return layers.Conv2D(filters, kernel_size, padding=padding, activation=activation)
|
class AE(models.Model):
def __init__(self, org_shape=(1, 28, 28)):
original = layers.Input(shape=org_shape)
x = Conv2D(4, (3, 3))(original)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3))(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
... |
def show_ae(autoencoder, data):
x_test = data.x_test
decoded_imgs = autoencoder.predict(x_test)
print(decoded_imgs.shape, data.x_test.shape)
if (backend.image_data_format() == 'channels_first'):
(N, n_ch, n_i, n_j) = x_test.shape
else:
(N, n_i, n_j, n_ch) = x_test.shape
x_test ... |
def main(epochs=20, batch_size=128):
data = DATA()
autoencoder = AE(data.input_shape)
history = autoencoder.fit(data.x_train, data.x_train, epochs=epochs, batch_size=batch_size, shuffle=True, validation_split=0.2)
plot_acc(history)
plt.show()
plot_loss(history)
plt.show()
show_ae(autoe... |
class Data():
def __init__(self, max_features=20000, maxlen=80):
((x_train, y_train), (x_test, y_test)) = imdb.load_data(num_words=max_features)
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
(self.x_train, self.y_tr... |
class RNN_LSTM(models.Model):
def __init__(self, max_features, maxlen):
x = layers.Input((maxlen,))
h = layers.Embedding(max_features, 128)(x)
h = layers.LSTM(128, dropout=0.2, recurrent_dropout=0.2)(h)
y = layers.Dense(1, activation='sigmoid')(h)
super().__init__(x, y)
... |
class Machine():
def __init__(self, max_features=20000, maxlen=80):
self.data = Data(max_features, maxlen)
self.model = RNN_LSTM(max_features, maxlen)
def run(self, epochs=3, batch_size=32):
data = self.data
model = self.model
print('Training stage')
print('==... |
class GAN():
def __init__(self, ni_D, nh_D, nh_G):
D = models.Sequential()
D.add(Dense(nh_D, activation='relu', input_shape=(ni_D,)))
D.add(Dense(nh_D, activation='relu'))
D.add(Dense(1, activation='sigmoid'))
model_compile(D)
G = models.Sequential()
G.add(... |
class Data():
def __init__(self, mu, sigma, ni_D):
self.real_sample = (lambda n_batch: np.random.normal(mu, sigma, (n_batch, ni_D)))
self.in_sample = (lambda n_batch: np.random.rand(n_batch, ni_D))
|
class Machine():
def __init__(self, n_batch=10, ni_D=100):
self.data = Data(0, 1, ni_D)
self.gan = GAN(ni_D=ni_D, nh_D=50, nh_G=50)
self.n_batch = n_batch
def train_D(self):
gan = self.gan
n_batch = self.n_batch
data = self.data
Real = data.real_sample... |
def Conv2D(filters, kernel_size, padding='same', activation='relu'):
return layers.Conv2D(filters, kernel_size, padding=padding, activation=activation)
|
class AE(models.Model):
def __init__(self, org_shape=(1, 28, 28)):
original = layers.Input(shape=org_shape)
x = Conv2D(4, (3, 3))(original)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3))(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
... |
def show_ae(autoencoder, data):
x_test = data.x_test
decoded_imgs = autoencoder.predict(x_test)
print(decoded_imgs.shape, data.x_test.shape)
if (backend.image_data_format() == 'channels_first'):
(N, n_ch, n_i, n_j) = x_test.shape
else:
(N, n_i, n_j, n_ch) = x_test.shape
x_test ... |
def main(epochs=20, batch_size=128):
data = DATA()
autoencoder = AE(data.input_shape)
history = autoencoder.fit(data.x_train, data.x_train, epochs=epochs, batch_size=batch_size, shuffle=True, validation_split=0.2)
plot_acc(history)
plt.show()
plot_loss(history)
plt.show()
show_ae(autoe... |
def ANN_models_func(Nin, Nh, Nout):
x = layers.Input(shape=(Nin,))
h = layers.Activation('relu')(layers.Dense(Nh)(x))
y = layers.Activation('softmax')(layers.Dense(Nout)(h))
model = models.Model(x, y)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return... |
def ANN_seq_func(Nin, Nh, Nout):
model = models.Sequential()
model.add(layers.Dense(Nh, activation='relu', input_shape=(Nin,)))
model.add(layers.Dense(Nout, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
|
class ANN_models_class(models.Model):
def __init__(self, Nin, Nh, Nout):
hidden = layers.Dense(Nh)
output = layers.Dense(Nout)
relu = layers.Activation('relu')
softmax = layers.Activation('softmax')
x = layers.Input(shape=(Nin,))
h = relu(hidden(x))
y = sof... |
class ANN_seq_class(models.Sequential):
def __init__(self, Nin, Nh, Nout):
super().__init__()
self.add(layers.Dense(Nh, activation='relu', input_shape=(Nin,)))
self.add(layers.Dense(Nout, activation='softmax'))
self.compile(loss='categorical_crossentropy', optimizer='adam', metric... |
def Data_func():
((X_train, y_train), (X_test, y_test)) = datasets.mnist.load_data()
Y_train = np_utils.to_categorical(y_train)
Y_test = np_utils.to_categorical(y_test)
(L, W, H) = X_train.shape
X_train = X_train.reshape((- 1), (W * H))
X_test = X_test.reshape((- 1), (W * H))
X_train = (X_... |
def plot_loss(history):
plt.plot(history.history['loss'], ':k')
plt.plot(history.history['val_loss'], '-k')
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc=0)
|
def plot_acc(history):
plt.plot(history.history['acc'], ':k')
plt.plot(history.history['val_acc'], '-k')
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc=0)
|
def main():
Nin = 784
Nh = 100
number_of_class = 10
Nout = number_of_class
model = ANN_seq_class(Nin, Nh, Nout)
((X_train, Y_train), (X_test, Y_test)) = Data_func()
history = model.fit(X_train, Y_train, epochs=15, batch_size=100, validation_split=0.2)
performace_test = model.evaluate(X... |
class Machine(aicnn.Machine):
def __init__(self):
((X, y), (x_test, y_test)) = datasets.cifar10.load_data()
super().__init__(X, y, nb_classes=10)
|
def main():
m = Machine()
m.run()
|
class CNN(models.Sequential):
def __init__(self, input_shape, num_classes):
super().__init__()
self.add(layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
self.add(layers.Conv2D(64, (3, 3), activation='relu'))
self.add(layers.MaxPooling2D(pool_size=... |
class DATA():
def __init__(self):
num_classes = 10
((x_train, y_train), (x_test, y_test)) = datasets.mnist.load_data()
(img_rows, img_cols) = x_train.shape[1:]
if (backend.image_data_format() == 'channels_first'):
x_train = x_train.reshape(x_train.shape[0], 1, img_rows... |
class DNN(models.Sequential):
def __init__(self, Nin, Nh_l, Nout):
super().__init__()
self.add(layers.Dense(Nh_l[0], activation='relu', input_shape=(Nin,), name='Hidden-1'))
self.add(layers.Dense(Nh_l[1], activation='relu', name='Hidden-2'))
self.add(layers.Dense(Nout, activation=... |
def Data_func():
((X_train, y_train), (X_test, y_test)) = datasets.cifar10.load_data()
Y_train = np_utils.to_categorical(y_train)
Y_test = np_utils.to_categorical(y_test)
(L, W, H, C) = X_train.shape
X_train = X_train.reshape((- 1), (W * H))
X_test = X_test.reshape((- 1), (W * H))
X_train ... |
def main():
Nin = 784
Nh_l = [100, 50]
number_of_class = 10
Nout = number_of_class
((X_train, Y_train), (X_test, Y_test)) = Data_func()
model = DNN(Nin, Nh_l, Nout)
history = model.fit(X_train, y_train, epochs=10, batch_size=100, validation_split=0.2)
performace_test = model.evaluate(X... |
class DataSet():
def __init__(self, X, y, nb_classes, scaling=True, test_size=0.2, random_state=0):
'\n X is originally vector. Hence, it will be transformed\n to 2D images with a channel (i.e, 3D).\n '
self.X = X
self.add_channels()
X = self.X
(X_trai... |
class CNN(Model):
def __init__(model, nb_classes, in_shape=None):
model.nb_classes = nb_classes
model.in_shape = in_shape
model.build_model()
super().__init__(model.x, model.y)
model.compile()
def build_model(model):
nb_classes = model.nb_classes
in_sh... |
class Machine():
def __init__(self, X, y, nb_classes=2, fig=True):
self.nb_classes = nb_classes
self.set_data(X, y)
self.set_model()
self.fig = fig
def set_data(self, X, y):
nb_classes = self.nb_classes
self.data = DataSet(X, y, nb_classes)
print('data... |
def load_data(fname='international-airline-passengers.csv'):
dataset = pd.read_csv(fname, usecols=[1], engine='python', skipfooter=3)
data = dataset.values.reshape((- 1))
plt.plot(data)
plt.xlabel('Time')
plt.ylabel('#Passengers')
plt.title('Original Data')
plt.show()
data_dn = (((data... |
def get_Xy(data, D=12):
X_l = []
y_l = []
N = len(data)
assert (N > D), 'N should be larger than D, where N is len(data)'
for ii in range(((N - D) - 1)):
X_l.append(data[ii:(ii + D)])
y_l.append(data[(ii + D)])
X = np.array(X_l)
X = X.reshape(X.shape[0], X.shape[1], 1)
... |
class Dataset():
def __init__(self, fname='international-airline-passengers.csv', D=12):
data_dn = load_data(fname=fname)
(X, y) = get_Xy(data_dn, D=D)
(X_train, X_test, y_train, y_test) = model_selection.train_test_split(X, y, test_size=0.2, random_state=42)
(self.X, self.y) = (X... |
def rnn_model(shape):
m_x = layers.Input(shape=shape)
m_h = layers.LSTM(10)(m_x)
m_y = layers.Dense(1)(m_h)
m = models.Model(m_x, m_y)
m.compile('adam', 'mean_squared_error')
m.summary()
return m
|
class Machine():
def __init__(self):
self.data = Dataset()
shape = self.data.X.shape[1:]
self.model = rnn_model(shape)
def run(self, epochs=400):
d = self.data
(X_train, X_test, y_train, y_test) = (d.X_train, d.X_test, d.y_train, d.y_test)
(X, y) = (d.X, d.y)
... |
class Data():
def __init__(self, max_features=20000, maxlen=80):
((x_train, y_train), (x_test, y_test)) = imdb.load_data(num_words=max_features)
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
(self.x_train, self.y_tr... |
class RNN_LSTM(models.Model):
def __init__(self, max_features, maxlen):
x = layers.Input((maxlen,))
h = layers.Embedding(max_features, 128)(x)
h = layers.LSTM(128, dropout=0.2, recurrent_dropout=0.2)(h)
y = layers.Dense(1, activation='sigmoid')(h)
super().__init__(x, y)
... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.