1、keras多输入二分类与多分类
二分类
X11=X1.reshape(X1.shape[0],n_timesteps,X1.shape[1])
X22=X2.reshape(X2.shape[0],n_timesteps,X2.shape[1])
X33=X3.reshape(X3.shape[0],n_timesteps,X3.shape[1])
# y1=OneHotEncoder(sparse = False).fit_transform(y)
# y2 = y1.reshape(y1.shape[0],1,y1.shape[1])
y1 = np.reshape(np.array(clicks_all1_100000_new['gender']),(-1,1))
y2=OneHotEncoder(sparse = False).fit_transform(y1)
y3 = y2.reshape(y2.shape[0],1,y2.shape[1])
print(X11,y3)
# define two sets of inputs
inputA = Input(shape=(1,30))
inputB = Input(shape=(1,30))
inputC = Input(shape=(1,30))
# the first branch operates on the first input
x = Bidirectional(LSTM(60, return_sequences=True))(inputA)
# x = Dense(8, activation="relu")(inputA)
x = Dense(10, activation="relu")(x)
# x = Model(inputs=inputA, outputs=x)
# the second branch opreates on the second input
y = Bidirectional(LSTM(60, return_sequences=True))(inputB)
# y = Dense(32, activation="relu")(y)
y = Dense(10, activation="relu")(y)
# y = Model(inputs=inputB, outputs=y)
z = Bidirectional(LSTM(60, return_sequences=True))(inputC)
# x = Dense(8, activation="relu")(inputA)
z = Dense(10, activation="relu")(z)
# z = Model(inputs=inputC, outputs=z)
# combine the output of the two branches
combined = concatenate([x, y, z])
# combined = concatenate([x.output, y.output, z.output])
# apply a FC layer and then a regression prediction on the
# combined outputs
w = Dense(10, activation="relu")(combined)
w = Dense(2, activation="sigmoid")(w)
# our model will accept the inputs of the two branches and
model = Model(inputs=[inputA, inputB, inputC], outputs=[w])
model.compile(optimizer='rmsprop', loss='binary_crossentropy')
model.fit([X11, X22,X33], y3,
epochs=3, batch_size=30)
多分类
y1 = np.vstack(clicks_all1_100000_new[‘age’])
X_train1, X_test1, y_train1, y_test1 = train_test_split(X, y1, test_size=0.15, random_state=0)
X11=X1.reshape(X1.shape[0],n_timesteps,X1.shape[1])
X22=X2.reshape(X2.shape[0],n_timesteps,X2.shape[1])
X33=X3.reshape(X3.shape[0],n_timesteps,X3.shape[1])
# y1=OneHotEncoder(sparse = False).fit_transform(y)
# y2 = y1.reshape(y1.shape[0],1,y1.shape[1])
y11 = np.reshape(np.array(clicks_all1_100000_new['age']),(-1,1))
y2=OneHotEncoder(sparse = False).fit_transform(y11)
y3 = y2.reshape(y2.shape[0],1,y2.shape[1])
print(X11,y3)
# define two sets of inputs
inputA = Input(shape=(1,30))
inputB = Input(shape=(1,30))
inputC = Input(shape=(1,30))
# the first branch operates on the first input
x = Bidirectional(LSTM(60, return_sequences=True))(inputA)
# x = Dense(8, activation="relu")(inputA)
x = Dense(10, activation="relu")(x)
# x = Model(inputs=inputA, outputs=x)
# the second branch opreates on the second input
y = Bidirectional(LSTM(60, return_sequences=True))(inputB)
# y = Dense(32, activation="relu")(y)
y = Dense(10, activation="relu")(y)
# y = Model(inputs=inputB, outputs=y)
z = Bidirectional(LSTM(60, return_sequences=True))(inputC)
# x = Dense(8, activation="relu")(inputA)
z = Dense(10, activation="relu")(z)
# z = Model(inputs=inputC, outputs=z)
# combine the output of the two branches
combined = concatenate([x, y, z])
# combined = concatenate([x.output, y.output, z.output])
# apply a FC layer and then a regression prediction on the
# combined outputs
w = Dense(10, activation="softmax")(combined)
# w = Dense(2, activation="sigmoid")(w)
# our model will accept the inputs of the two branches and
model = Model(inputs=[inputA, inputB, inputC], outputs=w)
model.compile(optimizer='adam', loss='categorical_crossentropy')
model.fit([X11, X22,X33], y3,
epochs=3, batch_size=30)
2、tf2 lstm加attention
keras与tf1没有带attention
单分类
n_timesteps = 1
X_train2=X_train.reshape(X_train.shape[0],n_timesteps,X_train.shape[1])
y_train2=OneHotEncoder(sparse = False).fit_transform(y_train)
y_train3 = y_train2.reshape(y_train2.shape[0],1,y_train2.shape[1])
X_test2=X_test.reshape(X_test.shape[0],n_timesteps,X_test.shape[1])
y_test2=OneHotEncoder(sparse = False).fit_transform(y_test)
y_test3 = y_test2.reshape(y_test2.shape[0],1,y_test2.shape[1])
# X1 = np.vstack(cc_creative_id)
# X11=X1.reshape(X1.shape[0],n_timesteps,X1.shape[1])
# X11=X1.reshape(X1.shape[0],n_timesteps,X1.shape[1])
# y11 = np.reshape(np.array(clicks_all1_100000_new['gender']),(-1,1))
# y2=OneHotEncoder(sparse = False).fit_transform(y11)
# y3 = y2.reshape(y2.shape[0],1,y2.shape[1])
input1 = tf.keras.Input(shape=(1, 380))
# bilstm = Bidirectional(LSTM(120, return_sequences=True))(input1)
bilstm = Bidirectional(LSTM(120, return_sequences=True))(input1)
att = Attention()([bilstm,bilstm])
# pool1 = GlobalMaxPool1D()(att)
# pool2 =GlobalMaxPool1D()(pool1)
# con = Concatenate()([pool1,pool2])
den = Dense(2, activation='sigmoid')(att)
model= tf.keras.Model(inputs=[input1],outputs=[den])
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])
# model.fit(X11, y3, epochs=3, batch_size=2)
model.fit(X_train2, y_train3, epochs=3, batch_size=60)
model.save_weights('lstm_qq_sex_word15.h5')
scores = model.evaluate(X_test2,y_test3,verbose=1)
print('Test loss:',scores[0])
print('Test accuracy:',scores[1])
重要:对于自定义Model没有用Sequential 模型保存直接用save报错,暂时用save_weights保留参数,然后加载模型需要模型原来def函数妙笔阁:https://www.mbgtxt.com
def create_model():
input1 = tf.keras.Input(shape=(1, 380))
# bilstm = Bidirectional(LSTM(120, return_sequences=True))(input1)
bilstm = Bidirectional(LSTM(120, return_sequences=True))(input1)
att = Attention()([bilstm,bilstm])
# pool1 = GlobalMaxPool1D()(att)
# pool2 =GlobalMaxPool1D()(pool1)
# con = Concatenate()([pool1,pool2])
den = Dense(2, activation='sigmoid')(att)
model= tf.keras.Model(inputs=[input1],outputs=[den])
return model
model = create_model()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])
model.load_weights('lstm_qq_sex_word15.h5')
qq_test = np.concatenate((cc1_creative_id,cc1_ad_id,cc1_advertiser_id,cc1_product_id), axis=1)
qq_test1 = qq_test.reshape(qq_test.shape[0],1,qq_test.shape[1])
qq_sex_results1 = model.predict(qq_test1)
预测用不了predict_classes,只能predict出来再np.argmax,但这个维度需要先降维squeeze()
qq_sex_results2 = np.argmax(np.squeeze(qq_sex_results1),1)
sex_results = pd.DataFrame(qq_sex_results2+1)
sex_results['user_id'] = clicks_all1_100000_test['user_id']
sex_results