def build_model():
    model = Sequential()

    model.add(
        Conv2D(nb_filters, kernel_size=kernel_size, input_shape=shape_ord))
    model.add(Activation('relu'))

    model.add(Conv2D(nb_filters // 2, kernel_size=kernel_size))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=pool_size))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(128))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    model.add(Dense(nb_classes))
    model.add(Activation('softmax'))
Beispiel #2
0

train_data = dataframe_from_csvs(train_list)
test_data = dataframe_from_csvs(test_list)

train_data.to_csv('./Data/dacon/comp5/train.csv')
test_data.to_csv('./Data/dacon/comp5/test.csv')

train_data = pd.read_csv('./Data/dacon/comp5/train.csv', header=0, index_col=0)
test_data = pd.read_csv('./Data/dacon/comp5/test.csv', header=0, index_col=0)

# print(train_data,test_data)

# [550800 rows x 63 columns]
# [444600 rows x 63 columns]

scal = StandardScaler()
scal.fit(train_data)
train_data = scal.transform(train_data)
test_data = scal.transform(test_data)

x = train_data[:, 1:-4]
y = train_data[:, -3:]
print(x.shape, y.shape)
print(x[0, -1])
print(y[0, 0])
# print(x)
# print(y)
model = Sequential()
model.add(Conv1D)
Beispiel #3
0
epoch_count = 200

input_shape = (32,32)
num_channels = 3

(all_train_x, all_train_y), (all_test_x, all_test_y) = cifar10.load_data()

all_train_x = all_train_x.astype('float32')
all_test_x = all_test_x.astype('float32')

all_train_y = np_utils.to_categorical(all_train_y, classes_count)
all_test_y = np_utils.to_categorical(all_test_y, classes_count)

model = Sequential()

model.add(Convolution2D(32,3,3,border_mode = 'same', input_shape=all_train_x.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2,2))
model.add(Dropout(0,25))

model.add(Convolution2D(64,3,3,border_mode = 'same'))
model.add(Activation('relu'))
model.add(Convolution2D(64,3,3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(512))
model.add(Activation('relu'))
Beispiel #4
0
from keras.model import Sequential
from keras.layers import Dense

model = Sequential()
model.add(Dense(100, input_dim=128, init='uniform', activation='relu'))
model.add(Dense(100, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

model.fit(X_train,
          Y_train,
          validation_data=(X_test, Y_test),
          nb_epoch=500,
          batch_size=10,
          verbose=2)

#training accuracy
scores = model.evaluate(X_train, Y_train)
print("Accuracy: %.2f%%" % (scores[1] * 100))

#testing accuracy
scores2 = model.evaluate(X_test, Y_test)
print("Accuracy: %.2f%%" % (scores2[1] * 100))
Beispiel #5
0
Output = Test.iloc[:, 2].values

# Setting up dataframes for 10 - cross validation
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(NN_Data, Output, test_size = 0.1,random_state = 0)

# feature scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# NEURAL NETWORK TIME!

import keras
from keras.model import Sequential
from keras.layers import Dense


NN = Sequential()
# Input and 1st hidden layer

NN.add(Dense(output_dim = 2, init = 'uniform', activation = 'relu', input_dim = 3))

# Second hidden layer
NN.add(Dense(output_dim = 1, init = 'uniform', activation = 'relu'))

# training the NN
NN.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics = ['accuracy'])
NN.fit(X_train, y_train, batch_size = 10, nb_epoch = 100)
Beispiel #6
0
"""
Created on Wed Apr 10 12:13:40 2019

@author: AN20027664
"""

#import Keras lib
from keras.model import Sequential  #for sequential modeling for CNN
from keras.layers import Convolution2D  # for convolution layers
from keras.layers import MaxPooling2D  # for maxpooling
from keras.layers import Flatten  #for flattening
from keras.layers import Dense  # for full-connections in hidden layer

classifier = Sequential()
classifier.add(
    Convolution2D(32, 3, 3, input_shape=(64, 64, 3), activation='relu')
)  #32,3,3 is number of filters having 3X3 size and Input_shape 64X64 for RGB(3)
classifier.add(MaxPooling2D(pool_size(2, 2)))
classifier.add(
    Convolution2D(32, 3, 3, activation='relu')
)  #32,3,3 is number of filters having 3X3 size and Input_shape 64X64 for RGB(3)
classifier.add(MaxPooling2D(pool_size(2, 2)))
classifier.add(Dense(output_dim=128, activation='relu'))
classifier.add(Dense(output_dim=1, activation='sigmoid'))

#compliling
classifier.compile(optimizer='adam',
                   loss='binary_crossentropy',
                   metrics=['accuracy'])

from keras.preprosessing.image import ImageDataGenerator
Beispiel #7
0
                loss='hinge')

siamese.set_weight(siamese_theano)

get_feature100 = theano.function(siamese.layers[0].input,
                                  siamese.layers[-2].output)

feat_x = get_feature100(train_x)

knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(feat_x)

feat_neighbor = np.empty((n_samples, 6, feat_x.shape[1]))
for sample in xrange(n_samples):
    neighbor_x = knn.kneighbors(feat_x[sample])
    feat_neighbor[sample, :5, :] = neighbor_x
    feat_neighbor[sample, 5, :] = feat_x[sample]

rnn = Sequential()
rnn.add(LSTM(100, input_shape=(5, 100), activation='tanh'))
rnn.add(Dense(n_labels, activation='softmax'))
rnn.compile(optimizer='adam',
            loss='categorical_crossentropy',
            metrics=['accuracy'])

rnn.fit(feat_neighbor, train_y,
        batch_size=5,
        nb_epoch=10,
        verbose=1)

Beispiel #8
0
"""
Testing for easiness of making perceptron in python :)
"""
from keras.model import Sequential
from keras.layer import Dense
import numpy
numpy.random.seed(7)
dataset = numpy.loadtxt("/home/vaishnav/MLP/Wine data/train.csv",
                        delimiter=",")
n = input("Enter Number of Neurons\n")
X = dataset[:, :13]
Y = dataset[:, 13]
model = Sequential()
model.add(Dense(14, input_dim=13, init="uniform", activation='sigmoid'))
model.add(Dense(n, init="uniform", activation="sigmoid"))
model.add(Dense(3, init="uniform", activation="sigmoid"))
    def create_model_dynamic_rasha(self,x_train,y_train,x_val,y_val):
        
        from hyperas.distributions import choice
        model = Sequential()
        model.add(Conv2D(filters= 32, kernel_size=(3, 3),padding="same", activation='relu',kernel_initializer='uniform', input_shape=(x_train.shape[1], x_train.shape[2], 1)))
        model.add(BatchNormalization())
        model.add(MaxPooling2D(pool_size=(2, 2),strides=(2, 2)))
        model.add(Conv2D(filters=64,kernel_size=(3, 3), activation='relu', kernel_initializer='uniform'))
	    model.add(Conv2D(filters=64,kernel_size=(3, 3), activation='relu', kernel_initializer='uniform'))
	    model.add(MaxPooling2D(pool_size=(2, 2)))
x_train = np.array(images)
y_train = np.array(measurements)

from keras.model import Sequential
from keras.layers import Flatten, dense, Lambda

model = Sequential()
# lambda layer for normalization
model.add(Lambda(Lambda x: x / 255.0, input_shape = (160, 320, 3)))
model.add(Flatten())
model.add(Dense(1))

model.compile(loss="mse", optimizer="adam")
model.fit(x_train, y_train, validation_split=0.2, shuffle=True, nb_epoch=5)
    def create_model_static_rasha(self,x_train):
        model = Sequential()
        model.add(Conv2D(filters= 32, kernel_size=(3, 3),padding="same", activation='relu',kernel_initializer='uniform', input_shape=(Cfg.image_width, Cfg.image_height, Cfg.channels)))
        model.add(BatchNormalization())
        model.add(MaxPooling2D(pool_size=(2, 2),strides=(2, 2)))
        model.add(Conv2D(filters=64,kernel_size=(3, 3), activation='relu', kernel_initializer='uniform'))
	    model.add(Conv2D(filters=64,kernel_size=(3, 3), activation='relu', kernel_initializer='uniform'))
	    model.add(MaxPooling2D(pool_size=(2, 2)))
Beispiel #12
0
from keras.model import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import SGD

model = Sequential()
model.add(Dense(64, input_dim=20, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=20, batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)
'''
model = Sequential()
# Dense(64) is a fully-connected layer with 64 hidden units.
# in the first layer, you must specify the expected input data shape:
# here, 20-dimensional vectors.
model.add(Dense(64, activation='relu', input_dim=20))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
Beispiel #13
0
# Building Convolution Neural Network

from keras.model import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense

# Initialize Convolution Neural Network

model = Sequential()

# Adding Convolution Layer - Get the Feature Map. Input Image X Feature Detector = Feature Map

model.add(Convolution2D(nb_filter,nb_row,nb_col,border_mode = 'same',input_shape = (3,256,256),activation = 'relu'))

""" nb_filter is the number of feature detectors. Start with 32~64~128~256
    nb_row is the number of rows in feature detector matrix Start with 3
    nb_col is the number of column in feature detector matrix Start with 3 
    border_mode how the feature detector handle border value
    input_shape tells us about what is the expected format of the images. Color image is a 3d image
    3~colors(red,blue,green),256~pixels,256~pixels 
    (3,256,256) is for Theano (256,256,3) is for Tensorflow 
    activation is the activation function for layers. Here we are using Rectifier Function """

    
# Pooling - Max Pool. Will be applied on each of the feature map. And will reduce the size of the feature maps
    
model.add(MaxPooling2D(poolsize = (2,2))    
Beispiel #14
0
train_target[0]


# In[15]:


from keras.model import Sequential
from keras.layers import Dense,Flatten

model = Sequential() #creating a new sequaentail type empty NN model

#we cant directly apply the 2D image to te NN -> so have to flatten the image-> 1x (28x28)
#convert 2D image to flatten layer
#784 neurons for the first layer
#this is the first inpiut layer
model.add(Flatten(input_shape=(28,28)))

#this is a classification problem 
model.add(Dense(512,activate='relu'))
model.add(Dense(256,activate='relu'))
model.add(Dense(128,activate='relu'))
model.add(Dense(64,activate='relu'))
model.add(Dense(10,activate='softmax')) #here we have 10 types of numbers: 0-9

#compile the model
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
model.summary()


# In[ ]:
Beispiel #15
0
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_validate = X_validate.reshape(X_validate.shape[0], *(28, 28, 1))
X_train.shape
X_test.shape
X_validate.shape

import keras
from keras.model import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout
from keras.optimizers import Adam
from keras.callbacks import TensorBoard

cnn_model = Sequential()

cnn_model.add(Conv2D(32, 3, 3, input_shape = (28, 28, 1), activation = 'relu'))

cnn_model.add(MaxPooling2D(pool_size = (2,2)))
cnn_model.add(Flatter())
cnn_model.add(Dense(output_dim = 32, activation = 'relu'))
cnn_model.add(Dense(output_dim = 32, activation = 'sigmoid'))
cnn_model.compile(loss ='asparse_categorical_crossentropy', optimizersadam(lr=0.001), matrics =['accuracy'])
epochs = 50
cnn_model.fix(X_train, y_train,batch_size = 512, nb_epoch = epochs, verbose = 1, validation_data = (X_validate, y_validate))

#evaluating the model
evaluation = cnn_model.evaluate(X_test, y_test)
print('Test Accuracy : {:.3f}'.format(evaluation[1]))
predicted_classes = cnn_model.predict_classes(X_test)
predicted_classes
Beispiel #16
0
# binary classes
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])

# mean square root
model.compile(optimizer='rmsprop', loss='mse')

# user defined metrics
def mean_pred(y_true, y_pred)
    return K.mean(y_pred)

model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy', mean_pred])

# train
# generate dataset
# binary
import numpy as np
data = np.random.random((1000, 100))
labels = np.random.randint(2, size=(1000, 1))

model = Squential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss = 'binary_crossentropy', metrics=['accuracy'])
model.fit(data, labels, epochs=10, batch_size=32)

# multiple
labels = np.random.randint(10, size=(1000, 1))
one_hot_labels = keras.utils.to_categorical(label, num_classes=10)
model.fit(data, one_hot_labels, epoches=10, batch_size=32)
Beispiel #17
0
from keras.model import Sequential
from keras.layers import Conv2D

model = Sequential()
model.add(
    Conv2D(filters=16,
           kernel_size=2,
           striders=2,
           padding='valid',
           activation='relu',
           input_shape=(200, 200, 1)))
model.summary()
Beispiel #18
0
x = trainDataset[:-1, :]

y = trainDataset[1:, :]

from keras.model import Sequential
from keras.layers import Dense
from keras.layers import LSTM

model = Sequential()

model.add(LSTM(units=32, activation='sigmoid', input_shape=(None, 1)))

model.add(Dense(units=1))

model.compile(optimizer='adam', loss='mean_squared_error')

model.fit(x, y, epochs=100, batch_size=32)

realStock = pd.read_csv('Google_Stock_Price_Test.csv')

realStock = realStock.iloc[:, 4:5].values

plt.plot(realStock, 'r')
Beispiel #19
0
def build_model(layers):
    model = Sequential()

    model.add(
        LSTM(input_shape=(layers[1], layers[0]),
             output_dim=layers[1],
             return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(layers[2], return_sequences=False))
    model.add(Dropout(0.2))

    model.add(Dense(output_dim=layers[3]))
    model.add(Activation("linear"))

    start = time.time()
    model.compile(loss="mse", optimizer="rmsprop")
    print("> Compilation Time : ", time.time() - start)
    return model
Beispiel #20
0
from keras.model import Sequential
from keras.layers import Dense
from keras import regularizers, optimizers
from datetime import datetime

# keep track of starting time
start = datetime.now()

###################### part ten -- set up model ###########################
classifier = Sequential()

"""
kernel_initializer: difference between 'uniform' and others
activation: difference between 'relu' and 'linear'
"""
classifier.add( Dense( units=128, kernel_initializer = 'uniform', activation = 'relu', input_dim = X_train.shape[1])) # first layer need to assign 'input_dim'
classifier.add( Dense( units=128, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add( Dense( units=128, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add( Dense( units=64, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add( Dense( units=64, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add( Dense( units=64, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add( Dense( units=32, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add( Dense( units=32, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add( Dense( units=32, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add( Dense( units=1, kernel_initializer = 'uniform', activation = 'linear'))

################### part eleven -- define optimizer ###############################
RMSprop = optimizers.RMSprop(lr=0.001) # can tune learning rate here

################## part twelve -- compile model #################################
classifier.compile(optimizer = RMSprop, loss = 'mean_squared_error', metrics = ['accuracy']) # can add more metrics, can change loss based on purpose
#Making sure that the values are float so that we can get decimal points after division
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')

#Normalizing the RGB codes by dividing it to the max RGB values
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print('NUmber of images in x_train', x_train.shape[0])
print('Number of images in x_test', x_test.shape[0])

#Import the required Keras modules containing model and layers
from keras.model import Sequential
from keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D
#Creating a sequential model and adding the layers
model = Sequential()
model.add(Conv2d(28, kernel_size=(3, 3), input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())  #Flatten the 2D arrays for the fully connected layers
model.add(Dense(128, activation=tf.nn.relu))
model.add(Dropout(0.2))
model.add(Dense(10, activation=tf.nn.softmax))

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
model.fit(x=x_train, y=y_train, epochs=10)

model.evauate(x_test, y_test)