2016-04-14 11 views
1
私は彼の講義ノートにKarpathyによって推奨重みの初期化を試したい

ウェイトの値を手動で初期化する方法は? (W = np.random.randn(N)/ SQRT:

推奨ヒューリスティックは、各ニューロンの重みベクトル を初期化することですn)は、nはその 入力

源の数である:http://cs231n.github.io/neural-networks-2/#init

私はPythonで初心者だ、と私はtはこれを実装する方法を知っている "ドン:/

weights = tf.Variable(??) 

助けてください?ランダムな値を持つベクトルの

weights = tf.Variable(10) 

:単一の値、使用のために...

答えて

2

shape = [784, 625] 
weights = tf.Variable(tf.random_normal(shape, stddev=0.01)/tf.sqrt(n)) 

あなたは変数を評価するsess.runする必要があることに注意してください。

また、他のランダムテンソルをチェックアウトしてください:https://www.tensorflow.org/versions/r0.8/api_docs/python/constant_op.html#random-tensors

+0

お返事ありがとうございました。私は、あなたが示したコード行のどこに 'np.random.randn(n)'があるのか​​分かりません。標準偏差引数で 'tf.random_normal'を使うのではなく、' np.random.randn(n) 'で重み行列の各重みを手動で設定したいと思います。これは達成できますか? – Kalanit

+0

私はtf.random.Xを使用します。 np.random.randn(n)をtf.randomに置き換えて、同じことをすることができます。 https://www.tensorflow.org/versions/r0.8/api_docs/python/constant_op.html#random-tensorsをご覧ください。 –

+0

@Kalanitちょっと興味がありますが、 'randn'は' random_normal'とは何が違うと思いますか? –

0
n = 10 
init_x = np.random.randn(n) 
x = tf.Variable(init_x) 
sess = tf.InteractiveSession() 
sess.run(tf.initialize_all_variables()) 
print(sess.run(x)) 
0

私は次のようにそれを行う:

self.w_full, self.b_full = [], [] 

    n_fc_layers = len(structure) 
    structure.insert(0, self.n_inputs) 

    with vs.variable_scope(self.scope): 
     for lr_idx in range(n_fc_layers): 
      n_in, n_out = structure[lr_idx], structure[lr_idx+1] 
      self.w_full.append(
       vs.get_variable(
        "FullWeights{}".format(lr_idx), 
        [n_in, n_out], 
        dtype=tf.float32, 
        initializer=tf.random_uniform_initializer(
         minval=-tf.sqrt(tf.constant(6.0)/(n_in + n_out)), 
         maxval=tf.sqrt(tf.constant(6.0)/(n_in + n_out)) 
        ) 
       ) 
      ) 

      self.b_full.append(
       vs.get_variable(
        "FullBiases{}".format(lr_idx), 
        [n_out], 
        dtype=tf.float32, 
        initializer=tf.constant_initializer(0.0) 
       ) 
      ) 

structure.insert(0, self.n_inputs) 

した後、あなたは[n_inputsを持っています、第1FC層サイズ、第2FC層サイズ...出力層サイズ]

関連する問題