質問編集履歴
10
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:
|
37
|
+
result = sess.run(op, feed_dict={holder: x})
|
37
38
|
print(result)
|
38
39
|
```
|
39
40
|
|
9
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]
|
23
|
+
output = tf.assign(output[0, 0], x[0, 0])
|
24
|
-
output = output[0, 1]
|
24
|
+
output = tf.assign(output[0, 1], x[0, 1])
|
25
|
-
output = output[1, 0]
|
25
|
+
output = tf.assign(output[1, 0], x[0, 2])
|
26
|
-
output = output[1, 1]
|
26
|
+
output = tf.assign(output[1, 1], x[1, 0])
|
27
|
-
output = output[2, 0]
|
27
|
+
output = tf.assign(output[2, 0], x[1, 1])
|
28
|
-
output = output[2, 1]
|
28
|
+
output = tf.assign(output[2, 1], x[1, 2])
|
29
29
|
return output
|
30
30
|
|
31
31
|
if __name__ == '__main__':
|
8
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
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
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
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.
|
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
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
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
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
title
CHANGED
File without changes
|
body
CHANGED
@@ -39,9 +39,9 @@
|
|
39
39
|
|
40
40
|
```
|
41
41
|
Traceback (most recent call last):
|
42
|
-
File "
|
42
|
+
File "run.py", line 23, in <module>
|
43
43
|
op = func(holder)
|
44
|
-
File "
|
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
エラーメッセージの修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -39,15 +39,25 @@
|
|
39
39
|
|
40
40
|
```
|
41
41
|
Traceback (most recent call last):
|
42
|
-
File "
|
42
|
+
File "run_classic.py", line 23, in <module>
|
43
|
-
|
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\
|
46
|
+
File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 258, in __call__
|
45
|
-
|
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\
|
50
|
+
File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 197, in <lambda>
|
47
|
-
|
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\
|
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
|
-
|
57
|
+
shape=shape)
|
58
|
+
File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 1853, in _init_from_args
|
50
|
-
|
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/ツールのバージョンなど)
|