问题描述
我用鸢尾花数据集做了一个最小可重复的例子.我制作了一个完整的神经网络,用于预测虹膜特征的最后一列.我也想输出目标(类别).因此,网络必须最小化两个不同的损耗函数(连续损耗和分类损耗).在下一个示例中,将全部设置为连续目标.但是,如何将其变成多输出问题?
I made a minimally reproducible example with the Iris dataset. I made an entire neural network that predicts the last column of the Iris features. I also want to output the target (category). So, the network must minimize two different loss functions (continuous, and categorical). All is set for the continuous target in the next example. But, how do I turn it into a multi-output problem?
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras import Model
from sklearn.datasets import load_iris
tf.keras.backend.set_floatx('float64')
iris, target = load_iris(return_X_y=True)
X = iris[:, :3]
y = iris[:, 3]
z = target
ds = tf.data.Dataset.from_tensor_slices((X, y, z)).batch(8)
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.d0 = Dense(16, activation='relu')
self.d1 = Dense(32, activation='relu')
self.d2 = Dense(1)
def call(self, x):
x = self.d0(x)
x = self.d1(x)
x = self.d2(x)
return x
model = MyModel()
loss_object = tf.keras.losses.MeanAbsoluteError()
optimizer = tf.keras.optimizers.Adam(learning_rate=1e-4)
loss = tf.keras.metrics.Mean(name='categorical loss')
error = tf.keras.metrics.MeanAbsoluteError()
@tf.function
def train_step(inputs, target):
with tf.GradientTape() as tape:
output = model(inputs)
run_loss = loss_object(target, output)
gradients = tape.gradient(run_loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
loss(run_loss)
error(target, output)
for epoch in range(50):
for xx, yy, zz in ds: # what to do with zz, the categorical target?
train_step(xx, yy)
template = 'Epoch {:>2}, MAE: {:>5.2f}'
print(template.format(epoch+1,
loss.result()))
loss.reset_states()
error.reset_states()
推荐答案
您可以执行以下操作.我希望您只需要一个多输出网络.在这里,我正在创建一个如下所示的模型.但是,即使您需要两个单独的模型,也应该能够轻松移植此模型.
You can do the following. I hope you just need a multi-output network. Here I'm creating a model that looks like follows. But even if you need two separate models, you should be able to easily port this.
x
| Dense(16)
x
| Dense(32)
x
Dense(1) / \ Dense(4, softmax)
/ \
(cont) y_1 y_2 (categorical)
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras import Model
from sklearn.datasets import load_iris
from tensorflow.keras.utils import to_categorical
import tensorflow.keras.backend as K
tf.keras.backend.set_floatx('float64')
import numpy as np
iris, target = load_iris(return_X_y=True)
K.clear_session()
X = iris[:, :3]
y = iris[:, 3]
z = target
ds = tf.data.Dataset.from_tensor_slices((X, y, z)).shuffle(buffer_size=150).batch(32)
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.d0 = Dense(16, activation='relu')
self.d1 = Dense(32, activation='relu')
self.d2_1 = Dense(1)
self.d2_2 = Dense(4, activation='softmax')
def call(self, x):
x = self.d0(x)
x = self.d1(x)
y_1 = self.d2_1(x)
y_2 = self.d2_2(x)
return y_1, y_2
model = MyModel()
loss_objects = [tf.keras.losses.MeanAbsoluteError(), tf.keras.losses.SparseCategoricalCrossentropy()]
optimizer = tf.keras.optimizers.Adam(learning_rate=1e-3)
acc = tf.keras.metrics.Accuracy(name='categorical loss')
loss = tf.keras.metrics.MeanAbsoluteError()
#error = tf.keras.metrics.MeanAbsoluteError()
@tf.function
def train_step(inputs, targets):
with tf.GradientTape() as tape:
outputs = model(inputs)
losses = [l(t, o) for l,o,t in zip(loss_objects, outputs, targets)]
gradients = tape.gradient(losses, model.trainable_variables)
#print(gradients)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
#optimizer.apply_gradients(zip(gradients[1], model.trainable_variables))
return outputs
for epoch in range(50):
for xx, yy, zz in ds: # what to do with zz, the categorical target?
outs = train_step(xx, [yy,zz])
res1 = acc.update_state(zz, np.argmax(outs[1], axis=1))
res2 = loss.update_state(yy, outs[0])
template = 'Epoch {:>2}, Accuracy: {:>5.2f}, MAE: {:>5.2f}'
print(template.format(epoch+1, acc.result(), loss.result()))
acc.reset_states()
loss.reset_states()
这篇关于如何为回归和分类创建多输出Tensorflow 2神经网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!