def base_model(): model = Sequential() model.add(Conv2D(32, (3, 3), padding='same', input_shape=x_train.shape[1:])) model.add(Activation('relu')) model.add(Conv2D(32,(3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Conv2D(64, (3, 3), padding='same')) model.add(Activation('relu')) model.add(Conv2D(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')) model.add(Dropout(0.5)) model.add(Dense(num_classes)) model.add(Activation('softmax')) sgd = SGD(lr = 0.1, decay=1e-6, momentum=0.9 nesterov=True) # Train model model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) return model
def build_model(self): model = Sequential() model.add(Dense(24, input_dim=self.state_size, activation='relu')) model.add(Dense(24, activation='relu')) model.add(Dense(self.action_size, activation='linear')) model.compile(loss='mse', optimizer=Adam(lr=self.learning_rate)) return model
from keras.models import Sequential from keras.layer import Dense import numpy as np x_train = np.array([1,2,3,4,5,6,7,8,9,10]) y_train = np.array([1,2,3,4,5,6,7,8,9,10]) x_test = np.array([101,102,103,104,105,106,107,108,109,110]) y_test = np.array([101,102,103,104,105,106,107,108,109,110]) model = Sequential() model.add(Dense(5, input_dim=1, activation='relu')) model.add(Dense(3)) model.add(Dense(1, activation='relu') model.compile(loss='mse', optimizer='adam', metrics=['accuracy']) model.fit(x_train, y_train, epochs=100, batch_size=1, validation_data= (x_train, y_train)) loss, acc = model.evaluate(x_test, y_test, batch_size=1) print("loss = ", loss) print("acc = ", acc)
""" 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"))
y_train = keras.utils.to_categorical(y_train, num_classes) #building the base vgg model such that features are extracted using the vggmodel. till the end of the convulution layers base_model=VGG16(weights='imagenet', include_top=False) x=preprocess_input(X)#using keras's built in function features=base_model.predict(x) np.save(open('features.npy', 'w'),features)#saving the model obtained after passing the input through the pretrained VGG network #building the bottleneck x_train=np.load(open('features.npy'))#loading the trained model top_model = Sequential() top_model.add(Flatten()) top_model.add(Dense(4096, activation='relu')) top_model.add(Dropout(0.9))#full layer with dropout- full6 top_model.add(Dense(4096, activation='relu')) top_model.add(Dropout(0.8)) top_model.add(Dense(20,activation='softmax'))#full layer with dropout-full7 #compiling the model with the adadelta optimizer top_model.compile(optimizer='Adadelta', loss='binary_crossentropy', metrics=['accuracy']) #fitting data to the model as per the parameters of the paper top_model.fit( x_train, y_train, batch_size=256, epochs=50,validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0) top_model.save('spatial_cnn.h5')#saving the weights obtained from the model
#!/usr/bin/env python # -*- coding: utf-8 -*- """ created by gjwei on 2017/9/28 """ from keras.layer import Input, Dense from keras.models import Model inputs = Input(shape=(784, )) x = Dense(128, activitions='relu', name='layer_1')(inputs) x = Dense(256, activitions='relu', name='layer_2')(x) predictions = Dense(10, activitions='softmax', name='output')(x) model = Model(inputs=inputs, outputs=predictions) model.compile(optimizer='rmsprop', loss='categroy_crossentropy', metrics=['accuracy'], )
#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (c) 2017 - xiongjiezk <*****@*****.**> import numpy as np from keras.layer import Input, Dense np.zeros() encoding_dim = 32 input_img = Input(shape=(784, )) encoded = Dense(encoding_dim, activation='relu')(input_img) decoded = Dense(784, activation='sigmoid')(encoded) autoencoder = Model(input=input_img, output=decoded)
#single layer neural network with Keras import numpy as numpy from keras.model import Sequenrial from keras.layer import Dense, Activation from keras.utils.visualize_util import plot model = Sequential() model.add(Dense(1, input_dim=500)) model.add(Activation(activation='sigmoid')) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) data = np.random.random((1000, 500)) labels = np.random.randint(2, size=(1000, 1)) score = model.evaluate(data, labels, verbose=0) print("Before Training: ", zip(model.metrics_names, score)) model.fit(data, labels, nb_epoch=10, batch_size=32, verbose=0) score = model.evaluate(data, labels, verbose=0) print("After training: ", zip(model.metrics_names, score)) plot(model, to_file='s1.png', show_shapes=True)