Tensorflow超简单模型?10个输入,1个输出,所以有11个可训练参数。

问题描述 投票:0回答:1

我对Tensorflow有点陌生,我用的是TensorflowJS,但欢迎发你的Python代码。

我想实现的是以下几个方面。

我想训练一个10个输入和1个输出的简单模型 我有10个尺寸一致的输入。[255,255].

输出的大小应该是 [255,255] 以及,并且应该根据一些权重来添加每个输入。所以会有10个权重(+偏置),输出只是输入的一个线性组合。

我想训练这10个权重,使其结果尽可能地接近于一个 验证矩阵 大小 [255,255]. 我认为绝对差值作为一个损失函数是最好的。

然而,我不知道如何在Tensorflow中制作这个可训练的模型?到目前为止,这是我得到的。

const model = tf.sequential();
model.add(tf.layers.dense({inputShape: [255,255], units: 10, activation: 'relu'}));
/* Prepare the model for training: Specify the loss and the optimizer. */
model.compile({loss: 'absoluteDifference', optimizer: 'momentum'});
tensorflow tensorflow.js
1个回答
1
投票

在python中,它将是这样的东西。

   model = keras.Sequential([
        keras.layers.Flatten(input_shape=(255, 255, 10)), # 10 inputs of 255x255 
        keras.layers.Dense(9, activation='relu'), 
        keras.layers.Dense(1, activation='sigmoid') #assuming it's binary classification, we use sigmoid
    ])

model.compile(optimizer='adam', 
              loss=tf.losses.BinaryCrossentropy(from_logits=True))

请注意,在TF2. 0中 absolutedifference 损失不存在。你必须使用 TF 1.X

你可以通过一个详细的例子,在 TF文件

编辑:模型总结

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_3 (Flatten)          (None, 650250)            0         
_________________________________________________________________
dense_5 (Dense)              (None, 9)                 5852259   
_________________________________________________________________
dense_6 (Dense)              (None, 1)                 10        
=================================================================
Total params: 5,852,269
Trainable params: 5,852,269
Non-trainable params: 0
_________________________________________________________________
© www.soinside.com 2019 - 2024. All rights reserved.