tensorflow2.3低阶API实现DNN和keras效果不同
用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论