teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

10

2020/05/04 08:09

投稿

CID8705
CID8705

スコア36

title CHANGED
File without changes
body CHANGED
@@ -33,7 +33,8 @@
33
33
  op = func(holder)
34
34
  with tf.Session() as sess:
35
35
  sess.run(tf.global_variables_initializer())
36
+ x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
36
- result = sess.run(op, feed_dict={holder: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]})
37
+ result = sess.run(op, feed_dict={holder: x})
37
38
  print(result)
38
39
  ```
39
40
 

9

2020/05/04 08:09

投稿

CID8705
CID8705

スコア36

title CHANGED
File without changes
body CHANGED
@@ -20,12 +20,12 @@
20
20
  output = tf.Variable(initial_value=tf.zeros([nbatch, 2], dtype=x.dtype), trainable=False)
21
21
  # 作成したテンソルの任意の要素に代入
22
22
  # output[0, 0] = x[0, 0]
23
- output = output[0, 0].assign(x[0, 0])
23
+ output = tf.assign(output[0, 0], x[0, 0])
24
- output = output[0, 1].assign(x[0, 1])
24
+ output = tf.assign(output[0, 1], x[0, 1])
25
- output = output[1, 0].assign(x[0, 2])
25
+ output = tf.assign(output[1, 0], x[0, 2])
26
- output = output[1, 1].assign(x[1, 0])
26
+ output = tf.assign(output[1, 1], x[1, 0])
27
- output = output[2, 0].assign(x[1, 1])
27
+ output = tf.assign(output[2, 0], x[1, 1])
28
- output = output[2, 1].assign(x[1, 2])
28
+ output = tf.assign(output[2, 1], x[1, 2])
29
29
  return output
30
30
 
31
31
  if __name__ == '__main__':

8

2020/05/04 08:07

投稿

CID8705
CID8705

スコア36

title CHANGED
File without changes
body CHANGED
@@ -20,12 +20,12 @@
20
20
  output = tf.Variable(initial_value=tf.zeros([nbatch, 2], dtype=x.dtype), trainable=False)
21
21
  # 作成したテンソルの任意の要素に代入
22
22
  # output[0, 0] = x[0, 0]
23
- output[0, 0].assign(x[0, 0])
23
+ output = output[0, 0].assign(x[0, 0])
24
- output[0, 1].assign(x[0, 1])
24
+ output = output[0, 1].assign(x[0, 1])
25
- output[1, 0].assign(x[0, 2])
25
+ output = output[1, 0].assign(x[0, 2])
26
- output[1, 1].assign(x[1, 0])
26
+ output = output[1, 1].assign(x[1, 0])
27
- output[2, 0].assign(x[1, 1])
27
+ output = output[2, 0].assign(x[1, 1])
28
- output[2, 1].assign(x[1, 2])
28
+ output = output[2, 1].assign(x[1, 2])
29
29
  return output
30
30
 
31
31
  if __name__ == '__main__':

7

2020/05/04 08:06

投稿

CID8705
CID8705

スコア36

title CHANGED
File without changes
body CHANGED
@@ -17,7 +17,7 @@
17
17
  nbatch = tf.shape(x)[0]
18
18
  # 任意の形状のテンソル作成
19
19
  # output = tf.zeros([nbatch, 2], dtype=x.dtype)
20
- output = tf.Variable(tf.zeros([nbatch, 2], dtype=x.dtype), trainable=False)
20
+ output = tf.Variable(initial_value=tf.zeros([nbatch, 2], dtype=x.dtype), trainable=False)
21
21
  # 作成したテンソルの任意の要素に代入
22
22
  # output[0, 0] = x[0, 0]
23
23
  output[0, 0].assign(x[0, 0])

6

2020/05/04 03:41

投稿

CID8705
CID8705

スコア36

title CHANGED
File without changes
body CHANGED
@@ -16,7 +16,7 @@
16
16
  # nbatch = x.get_shape().as_list()[0]
17
17
  nbatch = tf.shape(x)[0]
18
18
  # 任意の形状のテンソル作成
19
- # output = tf.zeros([nbatch, 2])
19
+ # output = tf.zeros([nbatch, 2], dtype=x.dtype)
20
20
  output = tf.Variable(tf.zeros([nbatch, 2], dtype=x.dtype), trainable=False)
21
21
  # 作成したテンソルの任意の要素に代入
22
22
  # output[0, 0] = x[0, 0]

5

2020/05/04 03:20

投稿

CID8705
CID8705

スコア36

title CHANGED
File without changes
body CHANGED
@@ -17,7 +17,7 @@
17
17
  nbatch = tf.shape(x)[0]
18
18
  # 任意の形状のテンソル作成
19
19
  # output = tf.zeros([nbatch, 2])
20
- output = tf.Variable(tf.zeros([nbatch, 2]), trainable=False)
20
+ output = tf.Variable(tf.zeros([nbatch, 2], dtype=x.dtype), trainable=False)
21
21
  # 作成したテンソルの任意の要素に代入
22
22
  # output[0, 0] = x[0, 0]
23
23
  output[0, 0].assign(x[0, 0])
@@ -29,9 +29,10 @@
29
29
  return output
30
30
 
31
31
  if __name__ == '__main__':
32
- holder = tf.placeholder(tf.int32, [None, 3])
32
+ holder = tf.placeholder(tf.float32, [None, 3])
33
33
  op = func(holder)
34
34
  with tf.Session() as sess:
35
+ sess.run(tf.global_variables_initializer())
35
36
  result = sess.run(op, feed_dict={holder: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]})
36
37
  print(result)
37
38
  ```

4

2020/05/04 03:18

投稿

CID8705
CID8705

スコア36

title CHANGED
File without changes
body CHANGED
@@ -29,7 +29,7 @@
29
29
  return output
30
30
 
31
31
  if __name__ == '__main__':
32
- holder = tf.placeholder(tf.int32, [None])
32
+ holder = tf.placeholder(tf.int32, [None, 3])
33
33
  op = func(holder)
34
34
  with tf.Session() as sess:
35
35
  result = sess.run(op, feed_dict={holder: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]})

3

2020/05/04 02:58

投稿

CID8705
CID8705

スコア36

title CHANGED
@@ -1,1 +1,1 @@
1
- tensorflowで低レベルな操作を行いたい
1
+ TensorFlowで低レベルな操作を行いたい
body CHANGED
@@ -2,12 +2,13 @@
2
2
 
3
3
  可変のバッチサイズを取得し,それを用いて新たな形状のテンソルを作成後,各要素に代入処理を行いたいです.
4
4
  ソースコードは簡単化のために書き換えています.
5
+ PyTorchで実装したソースコードを追記しました.
6
+ これをTensorFlowで実装したいです.
5
7
  よろしくお願いします.
6
8
 
7
9
  ### 該当のソースコード
8
10
 
9
11
  ```Python
10
- import numpy as np
11
12
  import tensorflow as tf
12
13
 
13
14
  def func(x):
@@ -35,6 +36,29 @@
35
36
  print(result)
36
37
  ```
37
38
 
39
+ ```Python
40
+ import torch
41
+
42
+ def func(x):
43
+ # バッチサイズを取得(可変)
44
+ nbatch = x.size(0)
45
+ # 任意の形状のテンソル作成
46
+ output = x.new_empty((nbatch, 2))
47
+ # 作成したテンソルの任意の要素に代入
48
+ output[0, 0] = x[0, 0]
49
+ output[0, 1] = x[0, 1]
50
+ output[1, 0] = x[0, 2]
51
+ output[1, 1] = x[1, 0]
52
+ output[2, 0] = x[1, 1]
53
+ output[2, 1] = x[1, 2]
54
+ return output
55
+
56
+ if __name__ == '__main__':
57
+ x = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
58
+ result = func(x)
59
+ print(result)
60
+ ```
61
+
38
62
  ### 発生している問題・エラーメッセージ
39
63
 
40
64
  ```

2

2020/05/03 09:25

投稿

CID8705
CID8705

スコア36

title CHANGED
File without changes
body CHANGED
@@ -39,9 +39,9 @@
39
39
 
40
40
  ```
41
41
  Traceback (most recent call last):
42
- File "run_classic.py", line 23, in <module>
42
+ File "run.py", line 23, in <module>
43
43
  op = func(holder)
44
- File "run_classic.py", line 10, in func
44
+ File "run.py", line 10, in func
45
45
  output = tf.Variable(tf.zeros([nbatch, 2]), trainable=False)
46
46
  File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 258, in __call__
47
47
  return cls._variable_v1_call(*args, **kwargs)

1

エラーメッセージの修正

2020/05/02 00:30

投稿

CID8705
CID8705

スコア36

title CHANGED
File without changes
body CHANGED
@@ -39,15 +39,25 @@
39
39
 
40
40
  ```
41
41
  Traceback (most recent call last):
42
- File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\framework\tensor_util.py", line 324, in _AssertCompatible
42
+ File "run_classic.py", line 23, in <module>
43
- fn(values)
43
+ op = func(holder)
44
+ File "run_classic.py", line 10, in func
45
+ output = tf.Variable(tf.zeros([nbatch, 2]), trainable=False)
44
- File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\framework\tensor_util.py", line 263, in inner
46
+ File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 258, in __call__
45
- _ = [_check_failed(v) for v in nest.flatten(values)
47
+ return cls._variable_v1_call(*args, **kwargs)
48
+ File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 219, in _variable_v1_call
49
+ shape=shape)
46
- File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\framework\tensor_util.py", line 264, in <listcomp>
50
+ File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 197, in <lambda>
47
- if not isinstance(v, expected_types)]
51
+ previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
52
+ File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variable_scope.py", line 2519, in default_variable_creator
53
+ shape=shape)
48
- File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\framework\tensor_util.py", line 248, in _check_failed
54
+ File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 262, in __call__
55
+ return super(VariableMetaclass, cls).__call__(*args, **kwargs)
56
+ File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 1688, in __init__
49
- raise ValueError(v)
57
+ shape=shape)
58
+ File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 1853, in _init_from_args
50
- ValueError: None
59
+ self._initial_value)
60
+ ValueError: initial_value must have a shape specified: Tensor("zeros:0", shape=(?, 2), dtype=float32)
51
61
  ```
52
62
 
53
63
  ### 補足情報(FW/ツールのバージョンなど)