用keras实现简单顺序模型

uxhixvfz  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(95)

我是Tensorflow的新手,我正在尝试遵循https://www.tensorflow.org/guide/keras/sequential_model上详细的步骤

#The Sequential model_1
#https://www.tensorflow.org/guide/keras/sequential_model

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

if (__name__=="__main__"):

    # Define Sequential model_1 with 3 layers
    model_1 = keras.Sequential(
        [
            layers.Dense(2, activation="relu", name="layer_1"),
            layers.Dense(3, activation="relu", name="layer_2"),
            layers.Dense(4, name="layer_3"),
        ]
    )
    # Call model_1 on a test input
    x = tf.ones((3, 3))
    y1 = model_1(x)

    model_1.layers
    ####################################################################

    # Create 3 discrete layers as above
    x = tf.ones((3, 3))
    layer1 = layers.Dense(2, activation="relu", name="layer1")
    layer2 = layers.Dense(3, activation="relu", name="layer2")
    layer3 = layers.Dense(4, name="layer3")
    y2 = layer3(layer2(layer1(x)))
    ####################################################################
    print('y1=', y1)
    print('y2=', y2)

根据TF指南,y1和y2应该相同。但这是程序的输出。有人知道是怎么回事吗?为什么y1和y2不同?

y1= tf.Tensor([[0. 0. 0. 0.][0. 0. 0. 0.][0. 0. 0. 0.]], shape=(3, 4), dtype=float32)
y2= tf.Tensor([[ 0.16045676  0.02784279  0.2826522  -0.2760685 ][ 0.16045676  0.02784279  0.2826522  -0.2760685 ][ 0.16045676  0.02784279  0.2826522  -0.2760685 ]], shape=(3, 4), dtype=float32)

如果代码修改为

model_1 = keras.Sequential(
        [
            layers.Dense(2, activation="relu", name="layer1"),
            layers.Dense(3, activation="relu", name="layer2"),
            layers.Dense(4, name="layer3"),
        ]
    )

输出将是这样的

y1= tf.Tensor([[-0.56710994  0.4444507   0.09539947  0.7368923 ][-0.56710994  0.4444507   0.09539947  0.7368923 ][-0.56710994  0.4444507   0.09539947  0.7368923 ]], shape=(3, 4), dtype=float32)
y2= tf.Tensor([[-0.05642081  0.04768222 -0.01716159  0.00080794][-0.05642081  0.04768222 -0.01716159  0.00080794][-0.05642081  0.04768222 -0.01716159  0.00080794]], shape=(3, 4), dtype=float32)

将哪个名称指定给图层是否重要??!!

e4yzc0pl

e4yzc0pl1#

实际上,这些是具有不同权重的不同层,因此输出是不同的。如果你想得到相同的输出,你应该使用这些层来构建模型。
固定代码:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

layer1 = layers.Dense(2, activation="relu", name="layer1")
layer2 = layers.Dense(3, activation="relu", name="layer2")
layer3 = layers.Dense(4, name="layer3")

model_1 = keras.Sequential([layer1, layer2, layer3])

x = tf.ones((3, 3))

y1 = model_1(x)
y2 = layer3(layer2(layer1(x)))

print('y1=', y1)
print('y2=', y2)

输出:

y1= tf.Tensor(
[[-1.0433494   1.3083773   0.47221014 -0.11679913]
 [-1.0433494   1.3083773   0.47221014 -0.11679913]
 [-1.0433494   1.3083773   0.47221014 -0.11679913]], shape=(3, 4), dtype=float32)
y2= tf.Tensor(
[[-1.0433494   1.3083773   0.47221014 -0.11679913]
 [-1.0433494   1.3083773   0.47221014 -0.11679913]
 [-1.0433494   1.3083773   0.47221014 -0.11679913]], shape=(3, 4), dtype=float32)

相关问题