gpt4 book ai didi

python - 输出指定为整数或浮点的 Tensorflow

转载 作者:太空宇宙 更新时间:2023-11-03 19:56:04 24 4
gpt4 key购买 nike

我正在研究将深度学习应用于回归问题,一些输出需要是整数,而其他输出可以是 float 。到目前为止,我已经构建了一个为所有输出返回 float 的神经网络,但我想进入下一步,为不同的输出实际返回整数和 float 。

Previously我问了一个问题,我提供了一个关于 y = m * x + b 的简单回归示例,我可以自己解决这个问题。在此示例中,如何更改代码以确保 b 为整数,而 m 为 float ?

#!/usr/bin/env python3

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

#################
### CONSTANTS ###
#################
ARANGE = (-5.0, 5.0) # Possible values for m in training data
BRANGE = (0.0, 10.0) # Possible values for b in training data
X_MIN = 1.0
X_MAX = 9.0
N = 10 # Number of grid points
M = 2 # Number of {(x,y)} sets to train on


def gen_ab(arange, brange):
""" mrange, brange are tuples of floats """
a = (arange[1] - arange[0])*np.random.rand() + arange[0]
b = (brange[1] - brange[0])*np.random.rand() + brange[0]

return (a, b)

def build_model(x_data, y_data):
""" Build the model using input / output training data
Args:
x_data (np array): Size (m, n*2) grid of input training data.
y_data (np array): Size (m, 2) grid of output training data.
Returns:
model (Sequential model)
"""
model = keras.Sequential()
model.add(layers.Dense(64, activation='relu', input_dim=len(x_data[0])))
model.add(layers.Dense(len(y_data[0])))

optimizer = tf.keras.optimizers.RMSprop(0.001)
model.compile(loss='mse', optimizer=optimizer, metrics=['mae', 'mse'])

return model


def gen_data(xs, arange, brange, m):
""" Generate training data for lines of y = m*x + b
Args:
xs (list): Grid points (size N1)
arange (tuple): Range to use for a (a_min, a_max)
brange (tuple): Range to use for b (b_min, b_max)
m (int): Number of y grids to generate
Returns:
x_data (np array): Size (m, n*2) grid of input training data.
y_data (np array): Size (m, 2) grid of output training data.
"""
n = len(xs)
x_data = np.zeros((m, 2*n))
y_data = np.zeros((m, 2))
for ix in range(m):
(a, b) = gen_ab(arange, brange)
ys = a*xs + b*np.ones(xs.size)
x_data[ix, :] = np.concatenate((xs, ys))
y_data[ix, :] = [a, b]

return (x_data, y_data)

def main():
""" Main routin """
# Generate the x axis grid to be used for all training sets
xs = np.linspace(X_MIN, X_MAX, N)

# Generate the training data
# x_train has M rows (M is the number of training samples)
# x_train has 2*N columns (first N columns are x, second N columns are y)
# y_train has M rows, each of which has two columns (a, b) for y = ax + b
(x_train, y_train) = gen_data(xs, ARANGE, BRANGE, M)

model = build_model(x_train, y_train)
model.fit(x_train, y_train, epochs=10, batch_size=32)
model.summary()

####################
### Test example ###
####################
(a, b) = gen_ab(ARANGE, BRANGE)
ys = a*xs + b*np.ones(xs.size)
rys = np.concatenate((xs, ys))
ab1 = model.predict(x_train)
ab2 = model.predict(np.array([rys]))

if __name__ == "__main__":
main()

最佳答案

我认为这是可能的,但实际上并不像听起来那么微不足道。不幸的是,您不能简单地让 NN 输出 int 和 float 并使用您正在使用的正常 MSE 损失,因为 int 值的离散性质阻止损失函数像优化器所需的那样连续可微。

如果真的想这样做,他们可以将 int 输出变量视为实际上是多类输出(同样对待 float 输出)。您需要根据这两个输出(多类+浮点)的组合来设计一个损失函数。您可以对多类输出进行 one-hot 编码,然后进行 softmax。一个有趣的复杂情况是,神经网络不会知道多类输出实际上是有序的(有序的,因为 1<2<3<4 等)。过去有一些有趣的尝试来帮助神经网络实现这一点(参见Neural Network Ordinal Classification for Age)。

关于python - 输出指定为整数或浮点的 Tensorflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59523661/

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