ぬの部屋(仮)
nu-no-he-ya
  •     123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
       1234
    567891011
    12131415161718
    19202122232425
    26272829   
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728     
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
     123456
    78910111213
    14151617181920
    21222324252627
    28      
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
    1234567
    891011121314
    15161718192021
    22232425262728
           
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30      
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
         12
    3456789
    10111213141516
    17181920212223
    242526272829 
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30      
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
        123
    45678910
    11121314151617
    18192021222324
    25262728   
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    15161718192021
    293031    
           
         12
    3456789
    10111213141516
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30      
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728     
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
          1
    2345678
    9101112131415
    16171819202122
    232425262728 
           
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
  • Tensorflow 2のKerasでxor回路を作る

    説明というか現状までの理解については次回以降にして、今はとにかく結果が得られるコードが欲しいのでそれらしい物を書いた。

    出力ノード1個の場合

    import tensorflow as tf
    import numpy as np
    
    from tensorflow.keras.optimizers import RMSprop
    
    
    # モデル作成
    # Define the model.
    model = tf.keras.models.Sequential([
        tf.keras.layers.InputLayer(input_shape=(2,)), # 入力層 要素数1、内容「2」のタプル
        tf.keras.layers.Dense(units=4, activation='sigmoid'),
        tf.keras.layers.Dense(units=4, activation='sigmoid'),
        tf.keras.layers.Dense(units=1, activation='linear') # 出力層 1出力。1出力の時はsoftmaxは使えない
    ])
    
    
    model.compile( optimizer=RMSprop(), loss='mse' )
    
    
    
    # トレーニング用入力
    XY = np.array( [[0.0,0.0],
                    [0.0,1.0],
                    [1.0,0.0],
                    [1.0,1.0]] )
    
    # 正解データ
    T = np.array( [[0.0],
                   [1.0],
                   [1.0],
                   [0.0]] )
    
    
    # トレーニング
    model.fit(XY, T, epochs=3000, batch_size=4)
    
    
    print("------------------------------")
    # 出力値をしきい値処理
    threshold = 0.5
    
    # 検証用データをモデルに入力し、出力(予測値)を取り出す
    predict_y = model.predict( np.array( [[0,0]] ) )
    print( predict_y , (predict_y > threshold).astype(np.int) )
    
    predict_y = model.predict( np.array( [[1,0]] ) )
    print( predict_y , (predict_y > threshold).astype(np.int) )
    
    predict_y = model.predict( np.array( [[0,1]] ) )
    print( predict_y , (predict_y > threshold).astype(np.int) )
    
    predict_y = model.predict( np.array( [[1,1]] ) )
    print( predict_y , (predict_y > threshold).astype(np.int) )
    
    [[-0.00275852]] [[0]]
    [[0.99734586]] [[1]]
    [[0.99620026]] [[1]]
    [[-0.00321453]] [[0]]

    出力ノード2個の場合(one hot 表現)

    import tensorflow as tf
    import numpy as np
    
    from tensorflow.keras.optimizers import RMSprop
    
    
    # モデル作成
    # Define the model.
    model = tf.keras.models.Sequential([
        tf.keras.layers.InputLayer(input_shape=(2,)), # 入力層 要素数1、内容「2」のタプル
        tf.keras.layers.Dense(units=4, activation='sigmoid'),
        tf.keras.layers.Dense(units=4, activation='sigmoid'),
        tf.keras.layers.Dense(units=2, activation='softmax') # 出力層 softmaxの時は2個以上の出力
    ])
    
    model.compile( optimizer=RMSprop(), loss='mse' )
    
    
    # トレーニング用入力
    XY = np.array( [[0.0,0.0],
                    [0.0,1.0],
                    [1.0,0.0],
                    [1.0,1.0]] )
    
    # 正解データ (One Hot表現)
    T = np.array([[0.0, 1.0],
                  [1.0, 0.0],
                  [1.0, 0.0],
                  [0.0, 1.0]])
    
    
    # トレーニング
    model.fit(XY, T, epochs=3000, batch_size=4)
    
    
    print("------------------------------")
    # 出力値をしきい値処理
    threshold = 0.5
    
    # 検証用データをモデルに入力し、出力(予測値)を取り出す
    predict_y = model.predict( np.array( [[0,0]] ) )
    print( predict_y , (predict_y > threshold).astype(np.int) )
    
    predict_y = model.predict( np.array( [[1,0]] ) )
    print( predict_y , (predict_y > threshold).astype(np.int) )
    
    predict_y = model.predict( np.array( [[0,1]] ) )
    print( predict_y , (predict_y > threshold).astype(np.int) )
    
    predict_y = model.predict( np.array( [[1,1]] ) )
    print( predict_y , (predict_y > threshold).astype(np.int) )
    
    
    [[0.00671812 0.99328184]] [[0 1]]
    [[0.9821952 0.01780475]] [[1 0]]
    [[0.9909299 0.00907017]] [[1 0]]
    [[0.0189229 0.98107713]] [[0 1]]

    参考

    Denseを調べます
    http://marupeke296.com/IKDADV_DL_No7_dense.html

    ご注文は TensorFlow 2.x ですか??
    https://hmx.hatenablog.jp/entry/2020/04/26/000000

    光軒の集い
    https://www.kouken-party.info/2017/10/25/python%E3%81%A8keras%E3%81%A7xor/

    kerasでOne-hotのラベルを作成する
    https://qiita.com/okj15/items/f081ac0928540f1579b5

    【Python/Keras】ニューラルネットで論理演算(XOR)の学習
    https://algorithm.joho.info/machine-learning/python-keras-neural-network-and-or/

    Cannot train a neural network solving XOR mapping
    https://stackoverflow.com/questions/34311586/cannot-train-a-neural-network-solving-xor-mapping