Make Training data
import random
import numpy as np
x_train = []
x1_train = []
y_train = []
atoms = [0,1]
p = [0.6,0.4]
for i in range(1000):
x_train.append([np.random.choice(atoms, p=p),np.random.choice(atoms, p=p)])
for i in range(1000):
x1_train.append([np.random.choice(atoms, p=p),np.random.choice(atoms, p=p)])
for i in x_train:
if 1 in i:
y_train.append([1])
else:
y_train.append([0])
Convert to numpy arrays to make them usable by keras
x_train = np.array(x_train)
x1_train = np.array(x_train)
y_train = np.array(y_train)
import tensorflow as tf
Normalize the data to make it better for the model to use
x_train = tf.keras.utils.normalize(x_train, axis = 1)
x1_train = tf.keras.utils.normalize(x_train, axis = 1)
y_train = tf.keras.utils.normalize(y_train, axis = 0)
Make model with dense layers
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128 , activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(1, activation = tf.nn.sigmoid))
Compile and train model on the 3 lists
model.compile(optimizer='adam',
loss='mean_absolute_percentage_error',
metrics=['accuracy'])
model.fit(x_train, x1_train, y_train, epochs = 10)
try this
from keras.layers import Input, Dense
from keras.models import Model
import keras
inputs1 = Input(shape=(784,))
inputs2 = Input(shape=(784,))
outputs_1 = Dense(64, activation='relu')(inputs1)
outputs_2 = Dense(64, activation='relu')(inputs2)
outputs = keras.layers.Concatenate([outputs_1, outputs_2])
predictions = Dense(10, activation='softmax')(outputs)
model = Model(inputs=[inputs1,inputs2], outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit([data1, data2], labels)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.