tensorflow2.3低阶API实现DNN和keras效果不同

发布于 2022-09-12 12:55:14 字数 2515 浏览 29 评论 0

用keras和tf2.3低阶API搭同样的网络,训练出来差很多,keras一上来loss就变小了,而tf2.3loss收敛很慢,这是什么原因呢?

下面是tf2.3代码~~~~
`class DNNModel(tf.Module):

def __init__(self,name = None, input_dim=50, hidden_units = [256, 512, 256]):
    super(DNNModel, self).__init__(name=name)
    self.w1 = tf.Variable(tf.random.truncated_normal([input_dim,hidden_units[0]]),dtype = tf.float32)
    self.b1 = tf.Variable(tf.zeros([1,hidden_units[0]]),dtype = tf.float32)
    self.w2 = tf.Variable(tf.random.truncated_normal([hidden_units[0],hidden_units[1]]),dtype = tf.float32)
    self.b2 = tf.Variable(tf.zeros([1,hidden_units[1]]),dtype = tf.float32)
    self.w3 = tf.Variable(tf.random.truncated_normal([hidden_units[1],hidden_units[2]]),dtype = tf.float32)
    self.b3 = tf.Variable(tf.zeros([1,hidden_units[2]]),dtype = tf.float32)
    self.w4 = tf.Variable(tf.random.truncated_normal([hidden_units[2],1]),dtype = tf.float32)
    self.b4 = tf.Variable(tf.zeros([1,1]),dtype = tf.float32)
# 正向传播

@tf.function(input_signature=[tf.TensorSpec(shape = [None, 50], dtype = tf.float32)])#shape = [None,13],
def __call__(self,x):

    x = tf.nn.relu(x@self.w1 + self.b1)
    x = tf.nn.relu(x@self.w2 + self.b2)
    x = tf.nn.relu(x@self.w3 + self.b3)
    y = x@self.w4 + self.b4
    return y
# 损失函数

@tf.function(input_signature=[tf.TensorSpec(shape = [None,1], dtype = tf.float32),
tf.TensorSpec(shape = [None,1], dtype = tf.float32)])

def loss_func(self,y_true,y_pred):
    return loss_obj(y_true, y_pred)
    #return  tf.reduce_mean(tf.compat.v1.squared_difference(y_true, y_pred))

# 评估指标(准确率)
@tf.function(input_signature=[tf.TensorSpec(shape = [None,1], dtype = tf.float32),
tf.TensorSpec(shape = [None,1], dtype = tf.float32)])

def metric_func(self,y_true,y_pred):
    acc = tf.reduce_mean(tf.compat.v1.squared_difference(y_true, y_pred))
    return acc`
   

这下面是keras代码
`def init_test_model():

model = Sequential()
model.add(Dense(256, input_dim=50, activation='relu'))
#self.model.add(Dropout(0.2))

model.add(Dense(512, activation='relu'))

#self.model.add(Dropout(0.2))

model.add(Dense(256, activation='relu'))

#model.add(Dropout(0.2))

model.add(Dense(1, activation='linear'))

model.compile(loss='mse', optimizer=opt, metrics=['mae'])  # Adam(lr=self.alpha, decay=self.alpha_decay)

return model`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文