Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

python - How to predict Y_train with two different X_train data

This is my first time working with python on machine learning.

I have a dataset that I split into a training dataset of length of 9000 and a test dataset of a length of 1000. I would like to predict articles categories (Y) by articles titles (X1) and abstracts (X2) X_train_pad1, X_test_pad1 (Titles), X_train_pad2,X_test_pad2 (Abstracts), Y_train_id and Y_test_id (Categories) are all of type numpy.ndarray, Their shape are respectively (9000, 36),(1000, 36),(9000, 1446),(1000, 1446),(9000,) and (1000,)

Below are the codes for the prediction of Y_train_id (i.e. Categories) by X_train_pad1 (i.e. Titles) and its evaluation

embed_dim= 128*2
dropout1 = 0.5
conv_filters= 128
conv_kernel = 2
maxpool_size = 2
dense_size = 128
batch_size = 128
epochs = 7 
vocab_size=5000
num_cat=96 #(number of categories)

model_cnn = tf.keras.models.Sequential()
# Embedding 
model_cnn.add(tf.keras.layers.Embedding(vocab_size,embed_dim, input_length= max_len))
# Dropout
model_cnn.add(tf.keras.layers.Dropout(dropout1))
# Convolution 1
model_cnn.add(tf.keras.layers.Conv1D(conv_filters, conv_kernel,padding='valid', strides= 1,activation='relu'))
# Maxpooling
model_cnn.add(tf.keras.layers.MaxPooling1D(maxpool_size))

#Flatten
model_cnn.add(tf.keras.layers.Flatten())
# Dense+ Activation
model_cnn.add(tf.keras.layers.Dense(dense_size,  activation='relu'))

# Classifieur (Dense + activation softmax)
model_cnn.add(tf.keras.layers.Dense(num_cat))
model_cnn.add(tf.keras.layers.Activation('softmax'))

# Compiler le modèle 
model_cnn.compile(loss='sparse_categorical_crossentropy', 
                  optimizer='adam', 
                  metrics= ['accuracy'])
# Afficher le summary du modèle
print(model_cnn.summary())

#Model fitting
model_cnn.fit(X_train_pad1, Y_train_id, batch_size = batch_size , epochs = epochs)

#model evaluation
model_cnn.evaluate(X_test_pad1, Y_test_id)

I would like to fit a model (with 2 two inputs) that allows me to predict categories by titles (X_train_pad1) and abstract (X_train_pad2) at the same time and to evaluate it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...