ぬの部屋(仮)
nu-no-he-ya
  • 1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        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      
         12
    3456789
    10111213141516
    17181920212223
    2425262728  
           
      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
           
       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
           
  • Joomla 3のSEF URLからindex.phpと数字を取り除く

    JoomlaのURLはデフォルトではとても複雑で、SEO Friendly URLの設定をすることでだいぶすっきりするが、それでもindex.phpや数字が残る。これを取り除く。

    手順

    システム→グローバル設定

    グローバル設定 → サイト
     → フレンドリURL ... はい
     → URLリライトの使用 ... はい

    URLリライトのために、サーバーに置いてある「htaccess.txt」のファイル名を「.htaccess」に変更
    ※もしInternal Server Error が出るなら以下を参照

    Joomla!備忘録

    .htaccess にファイル名を変更した内容を以下の部分を書き加えて下さい。

    14行目(Joomla!3.4.1 オリジナルの場合)

    Options +FollowSymlinks

    の先頭に(#)を追加して保存して下さい。

    https://joomla.mdeps.com/nmerror/urlrwerr.html

    記事→ 統合
    → URLルーティング ... モダン
    → URLからIDを削除する ... はい

    参考

    https://www.joomshaper.com/blog/making-joomla-site-seo-friendly-by-removing-id-from-urls

    https://www.joomlabeginner.com/blog/tutorials/116-how-to-remove-ids-and-numbers-from-joomla-urls

    https://joomla.mdeps.com/nmerror/urlrwerr.html

    Blender エッジをCurveに変換

    ただ Object → Convert To → Curve だけではカーブオブジェクトにはなっても曲線にならない。

    カーブにしてから種類をBezierカーブに変更する。

    F3 → Curve → Set Spline Type → Bezier

    Bezierではこれでもまだ曲線にならない。[v]を押し、ハンドルの種類をAlignedにすることで曲線になる。

    浮動小数点以外の値が入力されたら浮動小数点が入力されたことにするテンプレート関数

    目的

    以下のような関数があるとする。

    float divide(const float a, const float b) {
      return a / b;
    }
    

    諸事情によりテンプレート化を行ったとする。

    template<typename T>
    auto divide(const T a,const T b) {
      return a/b;
    }
    

    この時、整数が入力されたときにもまともな答えが出てほしい。

    template<typename T>
    auto divide(const T a,const T b) {
      return a/b;
    }
    
    int main()
    {
    
      float a = divide(5.0,10.0); // a == 0.5
    
      float b = divide(5,10);     // b == 0.0
    
      printf("%lf %lf", a,b);
    
      getchar();
    }
    

    方法

    こんなテンプレートを定義する

    //入力が浮動小数点の時はそのまま返す
    //それ以外の時はfloatを返す
    template<typename T, bool isint>struct IsRealT;
    template<typename T>struct IsRealT<T, true> { using type = T; };
    template<typename T>struct IsRealT<T, false> { using type = float; };
    
    //入力が整数ならfloatを返す
    template<typename T>
    struct RealIfInt {
    	using type = typename IsRealT<T, std::is_floating_point<T>::value>::type;
    };
    

    内部でキャストする。なんで普通にdoubleでキャストしないでテンプレートで切り分けなんかするのか、そもそもこの関数自体doubleで受け取ればいいんじゃないのか、と思うのだが、型が違うと随所でwarningが出て読みづらくなるというのがある。

    template<typename T>
    auto divide(const T a, const T b) {
    
      using realT = typename RealIfInt<T>::type;
    
      return static_cast<realT>(a) / static_cast<realT>(b);
    }
    
    int main()
    {
    
      auto a = divide(5.0, 10.0); // a == 0.5
    
      auto b = divide(5, 10);     // b == 0.5
    
      printf("%lf %lf", a, b);
    
      getchar();
    }
    

    正直、エラーの読みづらさをいうならテンプレートを使った時点で大変なことになるので説得力は微妙。

    NBytesのデータを扱うクラス

    PPMのような3byteのデータを扱うと画素のコピーとか素直にできない。あとループ時のメモリ計算が面倒くさい。sizeofでとれないので*3しなければならないが、PGM等に変えるとcharなので*3を外さなければいけない。

    かといって専用構造体を作るとそれはそれで使いにくい。そんなときNByteのデータを扱うようなデータ型が欲しい。

    NByteData.hpp

    #include<array>
    
    #include<cstring>
    
    template<size_t SIZE>
    class NByteData {
      std::array<unsigned char, SIZE> m_data;
    public:
      using ThisT = NByteData<SIZE>;
    
      unsigned char* data() { return m_data.data(); }
    
    
      ThisT& operator=(const ThisT& t) {
        for (size_t i = 0; i < m_data.size(); i++) {
          m_data[i] = t.m_data[i];
        }
        return *this;
      }
      bool operator==(const ThisT& t)const {
        for (size_t i = 0; i < SIZE; i++) {
          if (m_data[i] != t.m_data[i])
            return false;
        }
        return true;
      }
      bool operator!=(const ThisT& t)const {
        return !this->operator==(t);
      }
      NByteData() {}
      NByteData(const ThisT& src) {
        this->operator=(src);
      }
      NByteData(std::initializer_list<unsigned char> inits) {
        unsigned char* p = m_data.data();
        for (auto uc : inits) {
          *p = uc;
          ++p;
        }
      }
    };
    

    PPM出力の例

    #include <iostream>
    #include<vector>
    
    #pragma warning(disable:4996)
    
    #include "NByteData.hpp"
    
    
    // 3byte rgbの画素作成
    NByteData<3> uc3rgb(
      unsigned char r, 
      unsigned char g, 
      unsigned char b) {
    
      return NByteData<3>({r,g,b});
    }
    void pnmP3_Write(const char* const fname, const int vmax, const int width, const int height, const unsigned char* const p);
    
    
    int main()
    {
    
      std::vector<NByteData<3> > rgb_ppm;
    
      int width = 255;
      int height = 30;
      rgb_ppm.resize(width*height);
    
      for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
          rgb_ppm[y*width+x] = uc3rgb(x, 0, 0);
        }
      }
    
      pnmP3_Write(R"(C:\data\rgb.ppm)", 255, width, height, rgb_ppm.begin()->data());
    
    }
    /////////////////////////////////////////////////////////////////
    
    //! @brief PPM(RGB各1byte,カラー,テキスト)を書き込む
    //! @param [in] fname ファイル名
    //! @param [in] vmax 全てのRGBの中の最大値
    //! @param [in] width 画像の幅
    //! @param [in] height 画像の高さ
    //! @param [in] p 画像のメモリへのアドレス
    //! @details RGBRGBRGB....のメモリを渡すと、RGBテキストでファイル名fnameで書き込む
    void pnmP3_Write(const char* const fname, const int vmax, const int width, const int height, const unsigned char* const p) { // PPM ASCII
    
      FILE* fp = fopen(fname, "wb");
      fprintf(fp, "P3\n%d %d\n%d\n", width, height, vmax);
    
      size_t k = 0;
      for (size_t i = 0; i < (size_t)height; i++) {
        for (size_t j = 0; j < (size_t)width; j++) {
          fprintf(fp, "%d %d %d ", p[k * 3 + 0], p[k * 3 + 1], p[k * 3 + 2]);
          k++;
        }
        fprintf(fp, "\n");
      }
    
      fclose(fp);
    }
    

    PGM出力の例

    #include <iostream>
    #include<vector>
    
    #pragma warning(disable:4996)
    
    #include "NByteData.hpp"
    
    
    // 1byte グレイスケールの画素作成
    NByteData<1> uc1gray(unsigned char gray) {
    
      return NByteData<1>({gray});
    }
    void pnmP2_Write(const char* const fname, const int vmax, const int width, const int height, const unsigned char* const p);
    
    
    int main()
    {
    
      std::vector<NByteData<1> > gray_pgm;
    
      int width = 255;
      int height = 30;
      gray_pgm.resize(width*height);
    
      for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
          gray_pgm[y*width+x] = uc1gray(x);
        }
      }
    
      pnmP2_Write(R"(C:\data\gray.pgm)", 255, width, height, gray_pgm.begin()->data());
    
    }
    /////////////////////////////////////////////////////////////////
    
    //! @brief PPM(RGB各1byte,カラー,テキスト)を書き込む
    //! @param [in] fname ファイル名
    //! @param [in] vmax 全てのRGBの中の最大値
    //! @param [in] width 画像の幅
    //! @param [in] height 画像の高さ
    //! @param [in] p 画像のメモリへのアドレス
    //! @details RGBRGBRGB....のメモリを渡すと、RGBテキストでファイル名fnameで書き込む
    void pnmP2_Write(const char* const fname, const int vmax, const int width, const int height, const unsigned char* const p) { // PPM ASCII
    
      FILE* fp = fopen(fname, "wb");
      fprintf(fp, "P2\n%d %d\n%d\n", width, height, vmax);
    
      size_t k = 0;
      for (size_t i = 0; i < (size_t)height; i++) {
        for (size_t j = 0; j < (size_t)width; j++) {
          fprintf(fp, "%d ", p[k]);
          k++;
        }
        fprintf(fp, "\n");
      }
    
      fclose(fp);
    }
    

    Tensorflow 2のKerasで重みの取得と設定(2)

    Kerasでget_weights , set_weights。もうちょっと複雑なモデルで動作確認する。

    プログラム

    import tensorflow as tf
    import numpy as np
    
    
    # Define the model.
    model = tf.keras.models.Sequential([
        tf.keras.layers.Input(shape=(1,)),
        tf.keras.layers.Dense(3, activation='linear'),
        tf.keras.layers.Dense(2, activation='linear'),
        tf.keras.layers.Dense(1, activation='linear')
    ])
    
    print("----------------")
    
    
    # レイヤー0の重み取得
    L0w = model.layers[0].get_weights()
    L1w = model.layers[1].get_weights()
    L2w = model.layers[2].get_weights()
    # 取得時の重みを表示
    print("default weight 0", L0w )
    print("default weight 1", L1w )
    print("default weight 2", L2w )
    
    
    # 重みを書き換え
    L0w[0][0][0] = 1.0
    L0w[0][0][1] = 2.0
    L0w[0][0][2] = 3.0
    
    L1w[0][0][0] = 4.0
    L1w[0][0][1] = 5.0
    L1w[0][1][0] = 6.0
    L1w[0][1][1] = 7.0
    L1w[0][2][0] = 8.0
    L1w[0][2][1] = 9.0
    
    L2w[0][0][0] = 10.0
    L2w[0][1][0] = 11.0
    print("----------------")
    
    # 重みをモデルにセット
    model.layers[0].set_weights(L0w)
    model.layers[1].set_weights(L1w)
    model.layers[2].set_weights(L2w)
    
    # モデルの重みが更新されていることを確認
    print("edited  weight 0", model.layers[0].get_weights() )
    print("edited  weight 1", model.layers[1].get_weights() )
    print("edited  weight 2", model.layers[2].get_weights() )
    
    print("----------------")
    print( " 0番 output の値" )
    for n in range(0,5):
    	predict_y = model.predict( np.array( [[n]] ) )
    	print( n , predict_y[0][0] )
    
    ----------------
    default weight 0 [array([[ 0.8050083 , -0.3852656 , -0.80590665]], dtype=float32), array([0., 0., 0.], dtype=float32)]
    default weight 1 [array([[ 0.6107633 , 0.51594055],
    [-0.7214887 , -0.10213959],
    [-0.45495993, 0.23035955]], dtype=float32), array([0., 0.], dtype=float32)]
    default weight 2 [array([[0.20898771],
    [0.69574296]], dtype=float32), array([0.], dtype=float32)]
    ----------------
    edited weight 0 [array([[1., 2., 3.]], dtype=float32), array([0., 0., 0.], dtype=float32)]
    edited weight 1 [array([[4., 5.],
    [6., 7.],
    [8., 9.]], dtype=float32), array([0., 0.], dtype=float32)]
    edited weight 2 [array([[10.],
    [11.]], dtype=float32), array([0.], dtype=float32)]
    ----------------
    0番 output の値
    0 0.0
    1 906.0
    2 1812.0
    3 2718.0
    4 3624.0

    この時のモデル

    model.layers[].get_weights()はレイヤーごと、ノードごとに重みを取ってくる。ノードとノードを繋ぐ線をエッジというが、重みはいわばエッジが持っているので、「現在のノード」→「次のノード」のペアで対象の重みの位置を表せる。

    [0][そのレイヤーのノード番号][次のレイヤーのノード番号]

    重みの書き換え

    以下のように重みを書き換えてみる。

    # 重みを書き換え
    L0w[0][0][0] = 1.0
    L0w[0][0][1] = 2.0
    L0w[0][0][2] = 3.0
    
    L1w[0][0][0] = 0.0
    L1w[0][0][1] = 1.0
    L1w[0][1][0] = 0.0
    L1w[0][1][1] = 0.0
    L1w[0][2][0] = 0.0
    L1w[0][2][1] = 1.0
    
    L2w[0][0][0] = 3.0
    L2w[0][1][0] = 4.0
    

    結果、重みは以下のように設定されている。

    バイアスは今回は0ということにして、伝達関数はLinearにしているので、入力値はノードに入ったら重みを掛けられてそのまま次のノードへ行く。重みが0だと0になるので以下のような足し算になり、結果値が16倍される関数になる。

    結果(最終出力のみ)

    0番 output の値
    0 0.0
    1 16.0
    2 32.0
    3 48.
    4 64.0

    Tensorflow 2のKerasで重みの取得と設定(1)

    Kerasで作ったモデルのノードそれぞれの重みを取得するにはget_weights、設定するにはset_weightsを使用する。

    import tensorflow as tf
    import numpy as np
    
    # Define the model.
    model = tf.keras.models.Sequential([
        tf.keras.layers.Input(shape=(1,)),
        tf.keras.layers.Dense(2, activation='linear')
    ])
    
    print("----------------")
    
    # レイヤー0の重み取得
    L0w = model.layers[0].get_weights()
    
    # 取得時の重みを表示
    print("default weight 0", L0w )
    
    print("----------------")
    
    # 重みを書き換え
    L0w[0][0][0] = 1.0
    L0w[0][0][1] = 2.0
    
    # 重みをモデルにセット
    model.layers[0].set_weights(L0w)
    
    # モデルの重みが更新されていることを確認
    print("edited  weight 0", model.layers[0].get_weights() )
    
    print("----------------")
    print( " 0番 output の値" )
    for n in range(0,5):
    	predict_y = model.predict( np.array( [[n]] ) )
    	print( n ,"→ f →" , predict_y[0][0] )
    
    print("--------------------")
    print( " 1番 output の値" )
    for n in range(0,5):
    	predict_y = model.predict( np.array( [[n]] ) )
    	print( n ,"→ f →" , predict_y[0][1] )
    

    出力

    ----------------
    default weight 0 [array([[-1.1593556 , 0.38860273]], dtype=float32), array([0., 0.], dtype=float32)]
    ----------------
    edited weight 0 [array([[1., 2.]], dtype=float32), array([0., 0.], dtype=float32)]
    ----------------
    0番 output の値
    0 → f → 0.0
    1 → f → 1.0
    2 → f → 2.0
    3 → f → 3.0
    4 → f → 4.0
    --------------------
    1番 output の値
    0 → f → 0.0
    1 → f → 2.0
    2 → f → 4.0
    3 → f → 6.0
    4 → f → 8.0

    やっていること

    まずモデルを作る。

    # Define the model.
    model = tf.keras.models.Sequential([
        tf.keras.layers.Input(shape=(1,)),
        tf.keras.layers.Dense(2, activation='linear')
    ])
    

    次にモデルの重みを取得する

    # レイヤー0の重み取得
    L0w = model.layers[0].get_weights()
    

    するとどうやら、図のような関係になっているらしい。

    ので、書き換えてモデルを更新する。

    # 重みを書き換え
    L0w[0][0][0] = 1.0
    L0w[0][0][1] = 2.0
    
    # 重みをモデルにセット
    model.layers[0].set_weights(L0w)
    

    するとこんな関係になる。

    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

    tensorflow 2で計算グラフ(超初歩)とtensorBoardで表示(超初歩) ほぼメモ

    この記事の目的はとにかく検索すると1.x系列の情報がまずヒットするので手の届くところに最小限の2.x系列で動くサンプルを置いておく事にある。

    例1

    import tensorflow as tf
    
    a = tf.constant(10.1, dtype=tf.float32, name="a")
    b = tf.constant(20.2, dtype=tf.float32, name="b")
    c = tf.Variable(30.3, dtype=tf.float32, name="c")
    d = tf.Variable(40.4, dtype=tf.float32, name="d")
    
    # @tf.functionを付けると関数のような形で計算グラフを書ける
    @tf.function
    def f():
    
      divop = tf.divide(a,b,name="a-div-b")
      addop = tf.math.add(divop,c,name="div-add-c")
      d.assign(addop,name="d-assign-add")
      
    
    # ここでrun
    f()
    
    tf.print(d)
    

    例2

    import tensorflow as tf
    
    a = tf.constant(10.1, dtype=tf.float32, name="a")
    b = tf.constant(20.2, dtype=tf.float32, name="b")
    c = tf.Variable(30.3, dtype=tf.float32, name="c")
    d = tf.Variable(40.4, dtype=tf.float32, name="d")
    
    # @tf.functionを使わない場合。
    
    divop = tf.divide(a,b,name="a-div-b")
    addop = tf.math.add(divop,c,name="div-add-c")
    d.assign(addop,name="d-assign-add")
    
    tf.print(d)
    

    TensorBoardで計算グラフを表示してみる

    @tf.functionで書かないTensorBoard用出力の方法がどうしてもわからなかったのでとりあえず使ったもの。

    この例ではグラフをカレントディレクトリの./output/に出力する

    import tensorflow as tf
    import os
    
    
    logdir = './output'
    writer = tf.summary.create_file_writer(logdir)
    ###################################
    
    
    a = tf.constant(10.1, dtype=tf.float32, name="a")
    b = tf.constant(20.2, dtype=tf.float32, name="b")
    c = tf.Variable(30.3, dtype=tf.float32, name="c")
    d = tf.Variable(40.4, dtype=tf.float32, name="d")
    
    @tf.function
    def f():
    
    
      divop = tf.divide(a,b,name="a-div-b")
      addop = tf.math.add(divop,c,name="div-add-c")
      d.assign(addop,name="d-assign-add")
      
    
    ###################################
    
    
    with writer.as_default():
      tf.summary.graph(f.get_concrete_function().graph)
    
      
    writer.close()
    

    以下のコマンドでtensorBoardを起動し、ブラウザでhttp://localhost:6006/ を開く。

    > tensorboard --logdir ./output

    なぜかconstantの名前が消えてx,yになる上にVariableは表示すらされない。散々調べたが表示する方法が見つからなかった。

    計算グラフの表示に関してはtf.summary.trace_on(graph=True,profiler=False) でもできる。ちなみにprofiler=Trueにしたら私の環境ではエラーが出たので試せていない。

    import tensorflow as tf
    import os
    
    logdir = './output'
    writer = tf.summary.create_file_writer(logdir)
    
    tf.summary.trace_on(graph=True,profiler=False)
    ###################################
    
    a = tf.constant(0.01, dtype=tf.float32, name="a")
    s = tf.Variable(0.0, dtype=tf.float32, name="s")
    v = tf.Variable(0.0, dtype=tf.float32, name="v")
    
    
    @tf.function
    def f():
      s.assign_add(a)
      v.assign(tf.sin(s))
    
    
    ###################################
    
    f() # 一度は計算グラフを走らせないと出力できない
    
    with writer.as_default():
    
      tf.summary.trace_export("summary", step=0, profiler_outdir="./output")
    tf.summary.trace_off()
      
    writer.close()
    

    TensorBoardで値の変化を見る

    tensorflowはニューラルネットワークを組むために作られた計算グラフ作成ツールであり(という理解をしている)、同じ計算グラフを何度もrunさせて重みを調整していく事を想定しているので、繰り返した場合の値の変化をtensorBoardで確認してみる。

    import tensorflow as tf
    import os
    
    logdir = './output'
    writer = tf.summary.create_file_writer(logdir)
    
    ###################################
    
    a = tf.constant(0.01, dtype=tf.float32, name="a")
    s = tf.Variable(0.0, dtype=tf.float32, name="s")
    v = tf.Variable(0.0, dtype=tf.float32, name="v")
    
    
    @tf.function
    def f():
      s.assign_add(a)
      v.assign(tf.sin(s))
    
    ###################################
    
    with writer.as_default():
      for STEP in range(1000): # 1000 回繰り返し
        f() # 計算グラフ実行
        tf.summary.scalar("s",s,step=STEP) # scalarを出力するときは、何回目のループなのかを表すstepを指定しなければいけない
        tf.summary.scalar("v",v,step=STEP)
        writer.flush()
    
      
    writer.close()
    

    Blender 2.93 → Blender 2.79 のデータ変換

    Blender 2.9からBlender 2.79へデータを変換する方法。Blender 2.93側でCtrl+C(コピー)→Blender 2.79側でCtrl+V(貼り付け)をすれば2.79に取り込むことができる。

    Blender 2.7系はデフォルトでBlender RenderだったりするのでうっかりCyclesにし忘れるとレンダリングできないので注意。

    Creating a realistic snow mountain in blender.. | Blender 2.9 のチュートリアルを試す

    0.Landscapeの設定

    Editモードで[U]キーを押してunwrapする

    1.背景の設定

    以下のHDRIをダウンロードしてWorldに適用する。その後、レンダリング結果の背景が空だけになるようにカメラと背景の関係を調整する

    https://hdrihaven.com/hdri/?h=snowy_park_01

    2.Landscapeに対してMaterialを設定

    以下からテクスチャをダウンロードする

    https://www.textures.com/download/PBR0234/133290

    マテリアルを設定

    レンダリング結果