wide&deep定义子类时出错

来源:2-13 子类API实现wide&deep模型

慕用4325741

2019-07-26

class WideDeepModel(keras.models.Model):
    def __init(self):
        super(WideDeepModel, self).__init()
        #定义模型
        self.hidden1_layer = keras.layers.Dense(30,activation="relu")
        self.hidden2_layer = keras.layers.Dense(30,activation="relu")
        self.output_layer = keras.layers.Dense(1)

    def call(self, input):
        hidden1 = self.hidden1_layer(input)
        hidden2 = self.hidden2_layer(hidden1)
        concat = keras.layers.concatenate([input, hidden2])
        output = self.output_layer(concat)
        return output

编译器输出:

c:\users\centros\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\keras\engine\network.py in build(self, input_shape)
    718                            'method accepts an `inputs` argument.')
    719         try:
--> 720           self.call(x, **kwargs)
    721         except (errors.InvalidArgumentError, TypeError):
    722           raise ValueError('You cannot build your model by calling `build` '

<ipython-input-15-105c96268538> in call(self, input)
      8 
      9     def call(self, input):
---> 10         hidden1 = self.hidden1_layer(input)
     11         hidden2 = self.hidden2_layer(hidden1)
     12         concat = keras.layers.concatenate([input, hidden2])

AttributeError: 'WideDeepModel' object has no attribute 'hidden1_layer'

写回答

1回答

慕用4325741

提问者

2019-07-26

自己傻了,init少了两下划线

0
0

Google老师亲授 TensorFlow2.0 入门到进阶

Tensorflow2.0实战—以实战促理论的方式学习深度学习

1849 学习 · 896 问题

查看课程