Ejemplo n.º 1
0
def create_model():
    model = Sequential()
    model.add(Conv2D(LAYER1_SIZE, activation="relu", kernel_size=(3, 3),
                     input_shape=(2, BOARD_SIZE, BOARD_SIZE),
                     data_format="channels_first",
                     kernel_regularizer=l2(L2_REGULARISATION),
                     padding='same'))
    model.add(Conv2D(LAYER1_SIZE, activation="relu", kernel_size=(3, 3),
                     data_format="channels_first",
                     kernel_regularizer=l2(L2_REGULARISATION),
                     padding='same'))
    model.add(MaxPooling2D((2, 2), data_format="channels_first"))
    model.add(Conv2D(LAYER1_SIZE * 2, activation="relu", kernel_size=(3, 3),
                     data_format="channels_first",
                     kernel_regularizer=l2(L2_REGULARISATION),
                     padding='same'))
    model.add(Conv2D(LAYER1_SIZE * 2, activation="relu", kernel_size=(3, 3),
                     data_format="channels_first",
                     kernel_regularizer=l2(L2_REGULARISATION),
                     padding='same'))
    model.add(MaxPooling2D((2, 2), data_format="channels_first"))
    model.add(Flatten())
    model.add(Dense(LAYER2_SIZE, activation='relu', kernel_regularizer=l2(L2_REGULARISATION)))
    model.add(Dense(1, activation='tanh'))

    optimizer = Adam(decay=DECAY, lr=LR)
    model.compile(loss='mse', optimizer=optimizer, metrics=['accuracy', 'mae'])
    model.summary()

    return model
Ejemplo n.º 2
0
def create_lstm_model(num_features):
    model = Sequential()
    # "Encode" the input sequence using an RNN, producing an output of HIDDEN_SIZE.
    # Note: In a situation where your input sequences have a variable length,
    # use input_shape=(None, num_feature).
    # By setting return_sequences to True, return not only the last output but
    # all the outputs so far in the form of (num_samples, timesteps,
    # output_dim). This is necessary as TimeDistributed in the below expects
    # the first dimension to be the timesteps.
    model.add(RNN(HIDDEN_SIZE, input_shape=(None, num_features), return_sequences=True))

    # Apply a dense layer to the every temporal slice of an input. For each of step
    # of the output sequence, decide which character should be chosen.
    model.add(layers.TimeDistributed(layers.Dense(len(LABEL_CLASS_MAPPING) + 1)))
    model.add(layers.Activation('softmax'))
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    model.summary()
    return model
print('Minimum review length: {}'.format(len(min((X_test + X_test), key=len))))


from keras.preprocessing import sequence
max_words = 500
X_train = sequence.pad_sequences(X_train, maxlen=max_words)
X_test = sequence.pad_sequences(X_test, maxlen=max_words)

from keras import Sequential
from keras.layers import Embedding, LSTM, Dense, Dropout
embedding_size=32
model=Sequential()
model.add(Embedding(vocabulary_size, embedding_size, input_length=max_words))
model.add(LSTM(100))
model.add(Dense(1, activation='sigmoid'))
print(model.summary())


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

batch_size = 64
num_epochs = 3
X_valid, y_valid = X_train[:batch_size], y_train[:batch_size]
X_train2, y_train2 = X_train[batch_size:], y_train[batch_size:]
model.fit(X_train2, y_train2, validation_data=(X_valid, y_valid), batch_size=batch_size, epochs=num_epochs)

scores = model.evaluate(X_test, y_test, verbose=0)
print('Test accuracy:', scores[1])
# from matplotlib.pyplot import plt
train_data=pd.read_csv('D:\sufe\A\contest_basic_train.tsv',sep='\t')
train_data=train_data.drop(['REPORT_ID',"ID_CARD",'LOAN_DATE'],1)
train_data=train_data.dropna()
# print(train_data.info())
X=train_data.drop(['Y'],1).as_matrix()#7
y=train_data['Y'].as_matrix()#1
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

model=Sequential()
model.add(Dense(14,input_shape=(7,)))
model.add(Activation('relu'))
model.add(Dense(1))
model.add((Dropout(0.3)))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()

model.fit(X_train,y_train,epochs=10000,batch_size=16)
t=model.predict(X_test)

rate=0

for i in range(len(t)):
    if t[i]==y_test[i]:
        rate+=1
    else:
        pass
rate=1.0*rate/len(t)

print(rate)
Ejemplo n.º 5
0
y = train[target_col].values

from sklearn.model_selection import train_test_split
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.30, random_state=42)



#Model
conda install keras
from keras import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(100, input_dim=11, activation= "relu"))
model.add(Dense(50, activation= "relu"))
model.add(Dense(1))
model.summary() #Print model Summary


model.compile(loss= "mean_squared_error" , optimizer="adam", metrics=["mean_squared_error"])


model.fit(X_train, y_train, epochs=10)

from sklearn.metrics import mean_squared_error
pred= model.predict(X_valid)
score = np.sqrt(mean_squared_error(y_valid,pred))
print (score)


X_test = test[features].values
Ejemplo n.º 6
0
                 activation='relu',
                   padding='same'))
autoencoder.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
autoencoder.add(Dropout(0.25))

# Decoder
autoencoder.add(UpSampling2D(size=(4,4)))
autoencoder.add(Conv2D(1, kernel_size=(5, 5), strides=(1, 1),
                 activation='relu',
                   padding='same'))

# print layer shapes
encoder = Sequential(layers=autoencoder.layers[0:5])

autoencoder.compile(optimizer='adadelta', loss='mean_squared_error', metrics=['accuracy'])
print(autoencoder.summary())

# Training
autoencoder.fit(X_train, X_train, epochs=10, batch_size=32)

# Evaluation
autoencoder.evaluate(X_train, X_train, batch_size=128)
autoencoder.evaluate(X_test, X_test, batch_size=128)


# based on autoencoders.ipynb
n=5
for k in range(n):
    ax = plt.subplot(2, n, k + 1)
    X = X_test[k:k+1,:].reshape((28,28))
    X *= 255
Ejemplo n.º 7
0
class SeizeNet:
    def __init__(self, channels):
        self.model = None
        self.channels = channels
        self.mc = ModelCheckpoint('best_model_{}.h5'.format(self.channels),
                                  monitor='val_loss',
                                  mode='min',
                                  save_best_only=True)

        self.build_model()

        print("STARTING CREATING MODEL:")
        print('best_model_{}.h5'.format(self.channels))

    def build_model(self):
        self.model = Sequential()
        self.model.add(
            Conv2D(8, (1, 10),
                   activation='relu',
                   input_shape=(1, WINDOW_SIZE, self.channels)))
        self.model.add(MaxPooling2D((1, 2)))
        self.model.add(Dropout(0.2))

        self.model.add(Conv2D(16, (1, 10), activation='relu'))
        self.model.add(BatchNormalization())
        self.model.add(MaxPooling2D((1, 2)))
        self.model.add(Dropout(0.2))

        self.model.add(Conv2D(32, (1, 10), activation='relu'))
        self.model.add(BatchNormalization())
        self.model.add(MaxPooling2D((1, 2)))
        self.model.add(Dropout(0.2))

        self.model.add(Conv2D(64, (1, 10), activation='relu'))
        self.model.add(BatchNormalization())
        self.model.add(MaxPooling2D((1, 2)))
        self.model.add(Dropout(0.2))

        self.model.add(Flatten())
        self.model.add(Dense(50, activation='relu'))
        self.model.add(BatchNormalization())
        self.model.add(Dropout(0.5))
        self.model.add(Dense(1, activation='sigmoid'))

        return self.model

    def get_model_summary(self):
        self.model.summary()

    def build(self):
        self.model.compile(optimizer=Adam(lr=0.00041),
                           loss='binary_crossentropy',
                           metrics=['accuracy'])

    def fit_data(self, train_generator, test_generator):
        self.history = self.model.fit_generator(generator=train_generator,
                                                epochs=10,
                                                validation_data=test_generator,
                                                callbacks=[
                                                    self.mc,
                                                ])

    def load_model(self, file):
        self.model = load_model(file)
        self.model.summary()
Ejemplo n.º 8
0
def make_model(model_type,
               Xtrain,
               Ytrain,
               opt='adam',
               loss_func='mse',
               summary=False,
               binary=False):
    if binary:
        LAST_ACTIVATION = 'sigmoid'
    else:
        LAST_ACTIVATION = 'linear'
    print(model_type)
    if model_type is ModelType.SIMPLE_LSTM:
        print(model_type)
        # Single cell LSTM
        model = Sequential()
        model.add(
            LSTM(units=100,
                 activation='relu',
                 name='first_lstm',
                 recurrent_dropout=0.1,
                 input_shape=(Xtrain.shape[1], Xtrain.shape[2])))
        model.add(Dense(1, activation=LAST_ACTIVATION))

    elif model_type is ModelType.STACKED_LSTM:
        # Stacked LSTM
        model = Sequential()
        model.add(
            LSTM(100,
                 activation='relu',
                 return_sequences=True,
                 recurrent_dropout=0.1,
                 input_shape=(Xtrain.shape[1], Xtrain.shape[2])))
        model.add(
            LSTM(50,
                 activation='relu',
                 return_sequences=True,
                 recurrent_dropout=0.1))
        model.add(LSTM(30, activation='relu', recurrent_dropout=0.2))
        model.add(Dense(1, activation=LAST_ACTIVATION))

    elif model_type is ModelType.BIDRECTIONAL_LSTM:
        # Bidirectional LSTM
        model = Sequential()
        model.add(
            Bidirectional(LSTM(100, return_sequences=True, activation='relu')))
        model.add(
            Bidirectional(LSTM(50, return_sequences=True, activation='relu')))
        model.add(Bidirectional(LSTM(20, activation='relu')))
        model.add(Dense(1, activation=LAST_ACTIVATION))

    elif model_type is ModelType.CNN:
        model = Sequential()
        model.add(
            Conv1D(filters=128,
                   kernel_size=2,
                   activation='relu',
                   name='extractor',
                   input_shape=(Xtrain.shape[1], Xtrain.shape[2])))
        model.add(Dropout(0.5))
        model.add(MaxPooling1D(pool_size=2))
        model.add(Conv1D(filters=64, kernel_size=2, activation='relu'))
        model.add(Dropout(0.5))
        model.add(MaxPooling1D(pool_size=2))
        model.add(Flatten())
        model.add(Dense(50, activation='relu'))
        model.add(Dense(1, activation=LAST_ACTIVATION))

    elif model_type is ModelType.CNN_LSTM:
        model = Sequential()
        model.add(
            Conv1D(filters=256,
                   kernel_size=5,
                   padding='same',
                   activation='relu',
                   input_shape=(Xtrain.shape[1], Xtrain.shape[2])))
        model.add(
            Conv1D(filters=256,
                   kernel_size=5,
                   padding='same',
                   activation='relu'))
        model.add(MaxPooling1D(pool_size=4))
        model.add(
            Conv1D(filters=256,
                   kernel_size=5,
                   padding='same',
                   activation='relu'))
        model.add(MaxPooling1D(pool_size=4))
        model.add(LSTM(100))
        model.add(Dropout(0.5))
        model.add(Dense(100))
        model.add(Dense(1, activation=LAST_ACTIVATION))

    elif model_type is ModelType.LSTM_AUTOENCODER:
        model = Sequential()
        model.add(
            Conv1D(filters=128,
                   kernel_size=2,
                   activation='relu',
                   name='extractor',
                   input_shape=(Xtrain.shape[1], Xtrain.shape[2])))
        model.add(Dropout(0.3))
        model.add(MaxPooling1D(pool_size=2))
        model.add(
            Bidirectional(
                LSTM(50,
                     activation='relu',
                     input_shape=(Xtrain.shape[1], Xtrain.shape[2]))))
        model.add(RepeatVector(10))
        model.add(Bidirectional(LSTM(50, activation='relu')))
        model.add(Dense(1))

    elif model_type is ModelType.DEEP_CNN:
        model = Sequential()
        model.add(
            TimeDistributed(Conv1D(filters=64,
                                   kernel_size=2,
                                   activation='relu'),
                            input_shape=(None, Xtrain.shape[1],
                                         Xtrain.shape[2])))
        model.add(
            TimeDistributed(
                Conv1D(filters=64, kernel_size=2, activation='relu')))
        model.add(TimeDistributed(Dropout(0.5)))
        model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
        model.add(TimeDistributed(Flatten()))
        model.add(LSTM(100))
        model.add(Dropout(0.5))
        model.add(Dense(100, activation='relu'))
        model.add(Dense(1, activation='softmax'))

    elif model_type is ModelType.GRU:
        model = Sequential()
        model.add(
            GRU(75,
                return_sequences=True,
                input_shape=(Xtrain.shape[1], Xtrain.shape[2])))
        model.add(GRU(units=30, return_sequences=True))
        model.add(GRU(units=30))
        model.add(Dense(units=1, activation=LAST_ACTIVATION))

    elif model_type is ModelType.GRU_CNN:
        inp_seq = Input(shape=(Xtrain.shape[1], Xtrain.shape[2]))
        x = Bidirectional(GRU(100, return_sequences=True))(inp_seq)
        x = AveragePooling1D(2)(x)
        x = Conv1D(100, 3, activation='relu', padding='same',
                   name='extractor')(x)
        x = Flatten()(x)
        x = Dense(16, activation='relu')(x)
        x = Dropout(0.5)(x)

        out = Dense(1, activation=LAST_ACTIVATION)(x)

        model = Model(inp_seq, out)

    else:
        print("ERROR ", model_type)
        return None

    model.compile(loss=loss_func, optimizer=opt)
    # fit network
    if summary:
        model.summary()
    return model