gpt4 book ai didi

python - 我怎样才能让我的模型接受 2 个张量作为输入。我尝试过使用合并层,但我没有完全让它工作

转载 作者:行者123 更新时间:2023-11-30 09:58:43 24 4
gpt4 key购买 nike

制作训练数据

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])

转换为 numpy 数组以供 keras 使用

x_train = np.array(x_train)
x1_train = np.array(x_train)
y_train = np.array(y_train)
import tensorflow as tf

对数据进行标准化,以便模型更好地使用

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)

制作具有密集层的模型

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))

在 3 个列表上编译和训练模型

model.compile(optimizer='adam',
loss='mean_absolute_percentage_error',
metrics=['accuracy'])
model.fit(x_train, x1_train, y_train, epochs = 10)

最佳答案

试试这个

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)

关于python - 我怎样才能让我的模型接受 2 个张量作为输入。我尝试过使用合并层,但我没有完全让它工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59949716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com