Exemplo n.º 1
0
model1 = Embedding(n_user, k, input_length=1)(input1)
model1 = Reshape((k,))(model1)
input2 = Input(shape=(1,))
model2 = Embedding(n_sub, k, input_length=1)(input2)
model2 = Reshape((k,))(model2)
model = Concatenate()([model1, model2])
model = Dropout(0.2)(model)
model = Dense(k, activation='relu')(model)
model = Dropout(0.5)(model)
model = Dense(int(k/4), activation='relu')(model)
model = Dropout(0.5)(model)
model = Dense(int(k/16), activation='relu')(model)
model = Dropout(0.5)(model)
output = Dense(1, activation='linear')(model)
model = Model([input1, input2], output)
model.compile(loss='mse', optimizer='adam', metrics=['mae', 'acc'])
SVG(model_to_dot(model).create(prog='dot', format='svg'))

train, test = cross_validation.train_test_split(data, test_size=0.1, random_state=1)
x_train = [train.uid, train.sid]
y_train = train.rating
x_test = [test.uid, test.sid]
y_test = test.rating
history = model.fit(x_train, y_train, batch_size=500, epochs=20, validation_data=(x_test, y_test))

save_path = './save/ml-deep/'
model.save(save_path + 'model.h5')
with open(save_path + 'history.pkl', 'wb') as file_history:
    pickle.dump(history.history, file_history)
pd.DataFrame(history.history, columns=['loss', 'val_loss']).head(20).transpose()
plot(history.history['loss'], label='loss')
Exemplo n.º 2
0
model1 = Reshape((k1, ))(model1)
input2 = Input(shape=(1, ))
model2 = Embedding(n_sub, k2, input_length=1)(input2)
model2 = Reshape((k2, ))(model2)
model = Concatenate()([model1, model2])
model = Dropout(0.2)(model)
model = Dense(k, activation='relu')(model)
model = Dropout(0.5)(model)
model = Dense(int(k / 4), activation='relu')(model)
model = Dropout(0.5)(model)
model = Dense(int(k / 16), activation='relu')(model)
model = Dropout(0.5)(model)
output = Dense(num_classes, activation='softmax')(model)
model = Model([input1, input2], output)
model.compile(loss=losses.categorical_crossentropy,
              optimizer='adam',
              metrics=['acc', acc_top2])
SVG(model_to_dot(model).create(prog='dot', format='svg'))

data.rating = data.rating - 1
train, test = cross_validation.train_test_split(data,
                                                test_size=0.1,
                                                random_state=1)
x_train = [train.uid, train.sid]
y_train = utils.to_categorical(train.rating, num_classes)
x_test = [test.uid, test.sid]
y_test = utils.to_categorical(test.rating, num_classes)
history = model.fit(x_train,
                    y_train,
                    batch_size=1000,
                    epochs=20,