【问题标题】:How can I create irregularly shaped networks in Tensorflow and Keras?如何在 Tensorflow 和 Keras 中创建不规则形状的网络?
【发布时间】:2020-11-11 15:07:10
【问题描述】:

我正在使用 tensorflow 2 和 keras 制作神经网络,但与我找到的所有教程不同,我的网络有点不规则形状:

它由一个输入层、7 个隐藏层和一个输出层组成。然而,输入和前 4 个隐藏层被分成两部分,然后这两部分在第 5 个隐藏层中收敛,从那时起网络正常运行。它是这样构建的,因为网络旨在比较两个位置并预测哪个更好。如果你对这个概念感兴趣,它基于this 研究论文。

我不知道如何在 keras 和 tensorflow 中制作这样的网络,但这是我的尝试:

import tensorflow as tf

x_train, z_train, y_train = list(int(i) for i in "100000000000010000000000001000000000000100000000000010000000001000000"
                                                 "000010000000000100000000000000001000000000001000000000001000000000001"
                                                 "000000000001000000000001000000000001000000000001000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000001000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000001000000000001000000000001000000000"
                                                 "001000000000000000000000001000000000001000000000001000000100000000000"
                                                 "010000000000001000000000000100000000000010000000001000000000010000000"
                                                 "00010000001111000000000000000000000000"), \
                            list(int(i) for i in "100000000000010000000000001000000000000100000000000010000000001000000"
                                                 "000010000000000100000000000000001000000000001000000000001000000000001"
                                                 "000000000001000000000001000000000001000000000001000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000001000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000001000000000001000000000001000000000"
                                                 "001000000000000000000000001000000000001000000000001000000100000000000"
                                                 "010000000000001000000000000100000000000010000000001000000000010000000"
                                                 "00010000001111000000000000000000000000"), [0, 1]

train_dataset = tf.data.Dataset

network_input = tf.keras.Input(shape=(1594,))
model_a = tf.keras.Sequential(
    [tf.keras.layers.Dense(600, activation="sigmoid"),
     tf.keras.layers.Dense(400, activation="sigmoid"),
     tf.keras.layers.Dense(200, activation="sigmoid")]
)
output1 = model_a(network_input[:797])

model_b = tf.keras.Sequential(
    [tf.keras.layers.Dense(600, activation="sigmoid"),
     tf.keras.layers.Dense(400, activation="sigmoid"),
     tf.keras.layers.Dense(200, activation="sigmoid")]
)
output2 = model_b(network_input[797:])

x = tf.keras.layers.Concatenate()([output1, output2])
model_c = tf.keras.Sequential(
    [tf.keras.layers.Dense(200, activation="sigmoid"),
     tf.keras.layers.Dense(100, activation="sigmoid"),
     tf.keras.layers.Dense(2, activation="sigmoid")]
)
output = model_c(x)

model = tf.keras.Model(network_input, output)

# compile the models
model.compile(optimizer='sgd', loss='binary_crossentropy', metrics=['accuracy'])

# run the models
x_train = tf.convert_to_tensor(x_train)
z_train = tf.convert_to_tensor(z_train)
y_train = tf.convert_to_tensor(y_train)

p = tf.keras.layers.Concatenate()([x_train, z_train]), y_train

model.fit(p)

这给出了以下错误:

/Library/Frameworks/Python.framework/Versions/3.8/bin/python3 "/Users/max/Desktop/ArmstrongChess/error recreation.py"
2020-11-02 21:29:53.387241: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-11-02 21:29:53.450960: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7ff74fd79dd0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-11-02 21:29:53.450979: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
Traceback (most recent call last):
  File "/Users/max/Desktop/ArmstrongChess/error recreation.py", line 65, in <module>
    model.fit(p)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 108, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1049, in fit
    data_handler = data_adapter.DataHandler(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1105, in __init__
    self._adapter = adapter_cls(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 282, in __init__
    raise ValueError(msg)
ValueError: Data cardinality is ambiguous:
  x sizes: 1594, 2
Please provide data which shares the same first dimension.

Process finished with exit code 1

基本上,我试图将其创建为三个网络,其中前两个馈入最后一个以复制上图。

谁能解释一下如何做到这一点?我有点卡住了。



这是我根据 tushv89 的回答尝试的代码。我添加了一些 cmets 让您了解情况:

import tensorflow as tf

# in my actual project I have a function that creates this training data from a textfile of game PGNs, so what I call is this: "x_train, z_train, y_train = randomize_data(["won", "drawn", "lost"])". The following is just the information for one game so I don't have to paste a massive function here.

x_train, z_train, y_train = list(int(i) for i in "100000000000010000000000001000000000000100000000000010000000001000000"
                                                 "000010000000000100000000000000001000000000001000000000001000000000001"
                                                 "000000000001000000000001000000000001000000000001000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000001000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000001000000000001000000000001000000000"
                                                 "001000000000000000000000001000000000001000000000001000000100000000000"
                                                 "010000000000001000000000000100000000000010000000001000000000010000000"
                                                 "00010000001111000000000000000000000000"), \
                            list(int(i) for i in "100000000000010000000000001000000000000100000000000010000000001000000"
                                                 "000010000000000100000000000000001000000000001000000000001000000000001"
                                                 "000000000001000000000001000000000001000000000001000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000001000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000000000000000000000000000000000000000"
                                                 "000000000000000000000000000000000001000000000001000000000001000000000"
                                                 "001000000000000000000000001000000000001000000000001000000100000000000"
                                                 "010000000000001000000000000100000000000010000000001000000000010000000"
                                                 "00010000001111000000000000000000000000"), [0, 1]

# your code, copy pasted from here to the next comment
input = tf.keras.Input(shape=(1594,))

inp1, inp2 = tf.keras.layers.Lambda(lambda x: tf.split(x, 2, axis=1))(input)

model_a = tf.keras.Sequential(
    [tf.keras.layers.Dense(600, activation="sigmoid"),
     tf.keras.layers.Dense(400, activation="sigmoid"),
     tf.keras.layers.Dense(200, activation="sigmoid")]
)
output1 = model_a(input)

model_b = tf.keras.Sequential(
    [tf.keras.layers.Dense(600, activation="sigmoid"),
     tf.keras.layers.Dense(400, activation="sigmoid"),
     tf.keras.layers.Dense(200, activation="sigmoid")]
)
output2 = model_b(input)

x = tf.keras.layers.Concatenate()([output1, output2])
model_c = tf.keras.Sequential(
    [tf.keras.layers.Dense(200, activation="sigmoid"),
     tf.keras.layers.Dense(100, activation="sigmoid"),
     tf.keras.layers.Dense(2, activation="sigmoid")]
)
output = model_c(x)

# your code ends here. 
# I create the model with the input and output variables from your code
model = tf.keras.Model(inputs=input, outputs=output)
# I compile the model with my preferred settings (although I have no idea which would be the optimised settings, all I know is that I want sigmoid so the predictions are between 0 and 1)
model.compile(optimizer='sgd', loss='binary_crossentropy', metrics=['accuracy'])

# I convert the training variables to tensors
x_train = tf.convert_to_tensor(x_train)
z_train = tf.convert_to_tensor(z_train)
y_train = tf.convert_to_tensor(y_train)

# I call the network
model.fit(tf.keras.layers.Concatenate()([x_train, z_train]), y_train)

错误代码:

Traceback (most recent call last):
  File "/Users/max/Desktop/ArmstrongChess/error recreation.py", line 61, in <module>
    model.fit(tf.keras.layers.Concatenate()([x_train, z_train]), y_train)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 108, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1049, in fit
    data_handler = data_adapter.DataHandler(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1105, in __init__
    self._adapter = adapter_cls(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 282, in __init__
    raise ValueError(msg)
ValueError: Data cardinality is ambiguous:
  x sizes: 1594
  y sizes: 2
Please provide data which shares the same first dimension.

【问题讨论】:

    标签: tensorflow keras


    【解决方案1】:

    不要使用标准切片,而是使用tf.split。干净多了。

    input = tf.keras.Input(shape=(1594,))
    
    inp1, inp2 = tf.keras.layers.Lambda(lambda x: tf.split(x, 2, axis=1))(input)
    
    model_a = tf.keras.Sequential(
        [tf.keras.layers.Dense(600, activation="sigmoid"),
         tf.keras.layers.Dense(400, activation="sigmoid"),
         tf.keras.layers.Dense(200, activation="sigmoid")]
    )
    output1 = model_a(input)
    
    model_b = tf.keras.Sequential(
        [tf.keras.layers.Dense(600, activation="sigmoid"),
         tf.keras.layers.Dense(400, activation="sigmoid"),
         tf.keras.layers.Dense(200, activation="sigmoid")]
    )
    output2 = model_b(input)
    
    x = tf.keras.layers.Concatenate()([output1, output2])
    model_c = tf.keras.Sequential(
        [tf.keras.layers.Dense(200, activation="sigmoid"),
         tf.keras.layers.Dense(100, activation="sigmoid"),
         tf.keras.layers.Dense(2, activation="sigmoid")]
    )
    output = model_c(x)
    

    【讨论】:

    • 当我尝试这样做时,它给出了以下错误:ValueError: Attempt to convert a value (&lt;built-in function input&gt;) with an unsupported type (&lt;class 'builtin_function_or_method'&gt;) to a Tensor. 那是我的输入有问题吗?
    • @Maxijazz 有趣!您能告诉我确切的输入、tf 版本以及您对此所做的任何更改(即使很小)?
    • @tushv89 tf 2,我添加了带有小 cmets 的完整代码,告诉你你的代码是什么,我添加了什么以及为什么。错误也发生了变化,原来我只是在错误的事情上调用了 convert_to_tensor。现在新的错误(完整的错误代码显示在更新的问题中)是ValueError: Data cardinality is ambiguous: x sizes: 1594 y sizes: 2 Please provide data which shares the same first dimension.
    猜你喜欢
    • 2015-11-06
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 2020-05-17
    相关资源
    最近更新 更多