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`

阅读 361
评论
    1 个回答

    我也使用tensorflow 低阶api实现过一些模型。也是存在自己实现的比高阶api在性能和训练速度差的情况,但是差的不是很离谱,基本效果是差不多的.以上是我的经验,供参考。

      撰写回答

      登录后参与交流、获取后续更新提醒

      相似问题
      推荐文章