如何在张量流中使用maxout激活函数?

时间:2016-10-10 09:04:58

标签: tensorflow

我想在tensorflow中使用maxout激活函数,但我不知道应该使用哪个函数。

5 个答案:

答案 0 :(得分:5)

我发送了maxout的拉取请求,这里是链接:

https://github.com/tensorflow/tensorflow/pull/5528

代码如下:

  #include <iostream>
  using namespace std;
  int menumain;
  int oneplayer = 'X';


  char square1('1');
  char square2('2');
  char square3('3');
  char square4('4');
  char square5('5');
  char square6('6');
  char square7('7');
  char square8('8');
  char square9('9');

  void toggleplayers()
  {
  if (oneplayer == 'X')
  oneplayer = 'O';
  else
  oneplayer = 'X';
  };

  char win()
  {
  if (square1 == 'X' && square2 == 'X' &&square3 == 'X')
  return 'X';
  if (square4 == 'X' && square5 == 'X' &&square6 == 'X')
  return 'X';

  if (square7 == 'X' && square8 == 'X' &&square9 == 'X')
  return 'X';

  if (square1 == 'X' && square5 == 'X' && square9 == 'X')
  return 'X';
  if (square3 == 'X' && square5 == 'X' &&square7 == 'X')
  return 'X';

  if (square3 == 'X' && square6 == 'X' &&square9 == 'X')
  return 'X';
  if (square1 == 'X' && square4 == 'X' &&square7 == 'X')
  return 'X';

  if (square2 == 'X' && square5 == 'X' &&square8 == 'X')
  return 'X';

  if (square1 == 'O' && square2 == 'O' &&square3 == 'O')
  return 'O';
  if (square4 == 'O' && square5 == 'O' &&square6 == 'O')
  return 'O';

  if (square7 == 'X' && square8 == 'X' &&square9 == 'X')
  return 'X';

  if (square1 == 'O' && square5 == 'O' && square9 == 'O')
  return 'O';
  if (square3 == 'O' && square5 == 'O' &&square7 == 'O')
  return 'O';

  if (square3 == 'O' && square6 == 'O' &&square9 == 'O')
  return 'O';
  if (square1 == 'O' && square4 == 'O' &&square7 == 'O')
  return 'O';

  if (square2 == 'O' && square5 == 'O' &&square8 == 'O')
  return 'O';

  return '/';
  };

  int main()
  {
  int playerone, playertwo;


  cout << "tic tac toe" << endl;
  cout << "-----------" << endl;
  cout << "Start game (1)" << endl;
  cout << "Quit game (2)" << endl;

  cout << "Press 1 or 2 to proceed." << endl;
  cin >> menumain;
  if (menumain == 2)

  {
  return 0;
  }
  else
  {

   cout << "Player One, please enter your name: " << endl;
    // cin >> playerone; create char array or string and ask for an input;


   cout << "Player Two, please enter your name: " << endl;
   // cin >> playertwo;  create char array or string and ask for an input;


   cout << "Tic tac toe!" << endl;

   cout << "------------" << endl;

   cout << " " << square1 << " | " << square2 << " | " << square3 << " " << endl;

   cout << " " << square4 << " | " << square5 << " | " << square6 << " " << endl;

   cout << " " << square7 << " | " << square8 << " | " << square9 << " " <<     endl;

   cout << " " << endl;
   cout << playerone << endl; //here replace playerone with char array variable or string;
   cout << playertwo << endl; //here replace playerone with char array variable or string;








    while(1)
    {


    int playermove;
    cout << "Choose a number between 1-9 to place!" << endl;
    cin >> playermove;

    if (playermove == 1)
    square1 = oneplayer;

    else if (playermove == 2)
    square2 = oneplayer;
    else if (playermove == 3)
    square3 = oneplayer;

    else if (playermove == 4)
    square4 = oneplayer;
    else if (playermove == 5)
    square5 = oneplayer;
    else if (playermove == 6)
    square6 = oneplayer;

    else if (playermove == 7)
    square7 = oneplayer;
    else if (playermove == 8)
    square8 = oneplayer;
    else if (playermove == 9)
    square9 = oneplayer;

    cout << "Tic tac toe!" << endl;

    cout << "------------" << endl;

    cout << " " << square1 << " | " << square2 << " | " << square3 << " " << endl;

    cout << " " << square4 << " | " << square5 << " | " << square6 << " " << endl;

    cout << " " << square7 << " | " << square8 << " | " << square9 << " " <<     endl;

    cout << " " << endl;
    cout << playerone << endl;
    cout << playertwo << endl;





    if (win() == 'X')
    {

     cout << playerone << " wins!" << endl;
        break;
    }

    else if (win() == 'O')
    {

     cout << playertwo << " wins!" << endl;
        break;

     };
    toggleplayers();
     };



     return 0;
     };


     };

以下是它的工作原理:

github screenshot

答案 1 :(得分:1)

我认为没有最大限度的激活,但没有什么能阻止自己制造它。您可以执行以下操作。

with tf.variable_scope('maxout'):
  layer_input = ...
  layer_output = None
  for i in range(n_maxouts):
    W = tf.get_variable('W_%d' % d, (n_input, n_output))
    b = tf.get_variable('b_%d' % i, (n_output,))
    y = tf.matmul(layer_input, W) + b
    if layer_output is None:
      layer_output = y
    else:
      layer_output = tf.maximum(layer_output, y)

请注意,这是我刚在浏览器中编写的代码,因此可能存在语法错误,但您应该了解一般的想法。您只需执行多个线性变换,并在所有变换中取最大值。

答案 2 :(得分:0)

这段代码怎么样? 这似乎适用于我的测试。

def max_out(input_tensor,output_size):
shape = input_tensor.get_shape().as_list()
if shape[1] % output_size == 0:
    return tf.transpose(tf.reduce_max(tf.split(input_tensor,output_size,1),axis=2))
else:
    raise ValueError("Output size or input tensor size is not fine. Please check it. Reminder need be zero.")

我参考the following page中的图表。

答案 3 :(得分:0)

从版本1.4开始,您可以使用tf.contrib.layers.maxout

答案 4 :(得分:0)

Maxout是这样的一层,它可以为N * 1输入计算N * M输出,然后返回整个列的最大值,即最终输出也具有N * 1的形状。基本上,它使用多个线性拟合来模拟一个复杂的函数。