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`
以上是 tensorflow2.3低阶API实现DNN和keras效果不同 的全部内容, 来源链接: utcz.com/a/57281.html