質問編集履歴

10

2020/05/04 08:09

投稿

CID8705
CID8705

スコア36

test CHANGED
File without changes
test CHANGED
@@ -68,7 +68,9 @@
68
68
 
69
69
  sess.run(tf.global_variables_initializer())
70
70
 
71
+ x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
72
+
71
- result = sess.run(op, feed_dict={holder: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]})
73
+ result = sess.run(op, feed_dict={holder: x})
72
74
 
73
75
  print(result)
74
76
 

9

2020/05/04 08:09

投稿

CID8705
CID8705

スコア36

test CHANGED
File without changes
test CHANGED
@@ -42,17 +42,17 @@
42
42
 
43
43
  # output[0, 0] = x[0, 0]
44
44
 
45
- output = output[0, 0].assign(x[0, 0])
45
+ output = tf.assign(output[0, 0], x[0, 0])
46
46
 
47
- output = output[0, 1].assign(x[0, 1])
47
+ output = tf.assign(output[0, 1], x[0, 1])
48
48
 
49
- output = output[1, 0].assign(x[0, 2])
49
+ output = tf.assign(output[1, 0], x[0, 2])
50
50
 
51
- output = output[1, 1].assign(x[1, 0])
51
+ output = tf.assign(output[1, 1], x[1, 0])
52
52
 
53
- output = output[2, 0].assign(x[1, 1])
53
+ output = tf.assign(output[2, 0], x[1, 1])
54
54
 
55
- output = output[2, 1].assign(x[1, 2])
55
+ output = tf.assign(output[2, 1], x[1, 2])
56
56
 
57
57
  return output
58
58
 

8

2020/05/04 08:07

投稿

CID8705
CID8705

スコア36

test CHANGED
File without changes
test CHANGED
@@ -42,17 +42,17 @@
42
42
 
43
43
  # output[0, 0] = x[0, 0]
44
44
 
45
- output[0, 0].assign(x[0, 0])
45
+ output = output[0, 0].assign(x[0, 0])
46
46
 
47
- output[0, 1].assign(x[0, 1])
47
+ output = output[0, 1].assign(x[0, 1])
48
48
 
49
- output[1, 0].assign(x[0, 2])
49
+ output = output[1, 0].assign(x[0, 2])
50
50
 
51
- output[1, 1].assign(x[1, 0])
51
+ output = output[1, 1].assign(x[1, 0])
52
52
 
53
- output[2, 0].assign(x[1, 1])
53
+ output = output[2, 0].assign(x[1, 1])
54
54
 
55
- output[2, 1].assign(x[1, 2])
55
+ output = output[2, 1].assign(x[1, 2])
56
56
 
57
57
  return output
58
58
 

7

2020/05/04 08:06

投稿

CID8705
CID8705

スコア36

test CHANGED
File without changes
test CHANGED
@@ -36,7 +36,7 @@
36
36
 
37
37
  # output = tf.zeros([nbatch, 2], dtype=x.dtype)
38
38
 
39
- output = tf.Variable(tf.zeros([nbatch, 2], dtype=x.dtype), trainable=False)
39
+ output = tf.Variable(initial_value=tf.zeros([nbatch, 2], dtype=x.dtype), trainable=False)
40
40
 
41
41
  # 作成したテンソルの任意の要素に代入
42
42
 

6

2020/05/04 03:41

投稿

CID8705
CID8705

スコア36

test CHANGED
File without changes
test CHANGED
@@ -34,7 +34,7 @@
34
34
 
35
35
  # 任意の形状のテンソル作成
36
36
 
37
- # output = tf.zeros([nbatch, 2])
37
+ # output = tf.zeros([nbatch, 2], dtype=x.dtype)
38
38
 
39
39
  output = tf.Variable(tf.zeros([nbatch, 2], dtype=x.dtype), trainable=False)
40
40
 

5

2020/05/04 03:20

投稿

CID8705
CID8705

スコア36

test CHANGED
File without changes
test CHANGED
@@ -36,7 +36,7 @@
36
36
 
37
37
  # output = tf.zeros([nbatch, 2])
38
38
 
39
- output = tf.Variable(tf.zeros([nbatch, 2]), trainable=False)
39
+ output = tf.Variable(tf.zeros([nbatch, 2], dtype=x.dtype), trainable=False)
40
40
 
41
41
  # 作成したテンソルの任意の要素に代入
42
42
 
@@ -60,11 +60,13 @@
60
60
 
61
61
  if __name__ == '__main__':
62
62
 
63
- holder = tf.placeholder(tf.int32, [None, 3])
63
+ holder = tf.placeholder(tf.float32, [None, 3])
64
64
 
65
65
  op = func(holder)
66
66
 
67
67
  with tf.Session() as sess:
68
+
69
+ sess.run(tf.global_variables_initializer())
68
70
 
69
71
  result = sess.run(op, feed_dict={holder: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]})
70
72
 

4

2020/05/04 03:18

投稿

CID8705
CID8705

スコア36

test CHANGED
File without changes
test CHANGED
@@ -60,7 +60,7 @@
60
60
 
61
61
  if __name__ == '__main__':
62
62
 
63
- holder = tf.placeholder(tf.int32, [None])
63
+ holder = tf.placeholder(tf.int32, [None, 3])
64
64
 
65
65
  op = func(holder)
66
66
 

3

2020/05/04 02:58

投稿

CID8705
CID8705

スコア36

test CHANGED
@@ -1 +1 @@
1
- tensorflowで低レベルな操作を行いたい
1
+ TensorFlowで低レベルな操作を行いたい
test CHANGED
@@ -5,6 +5,10 @@
5
5
  可変のバッチサイズを取得し,それを用いて新たな形状のテンソルを作成後,各要素に代入処理を行いたいです.
6
6
 
7
7
  ソースコードは簡単化のために書き換えています.
8
+
9
+ PyTorchで実装したソースコードを追記しました.
10
+
11
+ これをTensorFlowで実装したいです.
8
12
 
9
13
  よろしくお願いします.
10
14
 
@@ -15,8 +19,6 @@
15
19
 
16
20
 
17
21
  ```Python
18
-
19
- import numpy as np
20
22
 
21
23
  import tensorflow as tf
22
24
 
@@ -67,6 +69,52 @@
67
69
  result = sess.run(op, feed_dict={holder: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]})
68
70
 
69
71
  print(result)
72
+
73
+ ```
74
+
75
+
76
+
77
+ ```Python
78
+
79
+ import torch
80
+
81
+
82
+
83
+ def func(x):
84
+
85
+ # バッチサイズを取得(可変)
86
+
87
+ nbatch = x.size(0)
88
+
89
+ # 任意の形状のテンソル作成
90
+
91
+ output = x.new_empty((nbatch, 2))
92
+
93
+ # 作成したテンソルの任意の要素に代入
94
+
95
+ output[0, 0] = x[0, 0]
96
+
97
+ output[0, 1] = x[0, 1]
98
+
99
+ output[1, 0] = x[0, 2]
100
+
101
+ output[1, 1] = x[1, 0]
102
+
103
+ output[2, 0] = x[1, 1]
104
+
105
+ output[2, 1] = x[1, 2]
106
+
107
+ return output
108
+
109
+
110
+
111
+ if __name__ == '__main__':
112
+
113
+ x = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
114
+
115
+ result = func(x)
116
+
117
+ print(result)
70
118
 
71
119
  ```
72
120
 

2

2020/05/03 09:25

投稿

CID8705
CID8705

スコア36

test CHANGED
File without changes
test CHANGED
@@ -80,11 +80,11 @@
80
80
 
81
81
  Traceback (most recent call last):
82
82
 
83
- File "run_classic.py", line 23, in <module>
83
+ File "run.py", line 23, in <module>
84
84
 
85
85
  op = func(holder)
86
86
 
87
- File "run_classic.py", line 10, in func
87
+ File "run.py", line 10, in func
88
88
 
89
89
  output = tf.Variable(tf.zeros([nbatch, 2]), trainable=False)
90
90
 

1

エラーメッセージの修正

2020/05/02 00:30

投稿

CID8705
CID8705

スコア36

test CHANGED
File without changes
test CHANGED
@@ -80,23 +80,43 @@
80
80
 
81
81
  Traceback (most recent call last):
82
82
 
83
- File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\framework\tensor_util.py", line 324, in _AssertCompatible
83
+ File "run_classic.py", line 23, in <module>
84
84
 
85
- fn(values)
85
+ op = func(holder)
86
86
 
87
- File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\framework\tensor_util.py", line 263, in inner
87
+ File "run_classic.py", line 10, in func
88
88
 
89
- _ = [_check_failed(v) for v in nest.flatten(values)
89
+ output = tf.Variable(tf.zeros([nbatch, 2]), trainable=False)
90
90
 
91
- File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\framework\tensor_util.py", line 264, in <listcomp>
91
+ File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 258, in __call__
92
92
 
93
- if not isinstance(v, expected_types)]
93
+ return cls._variable_v1_call(*args, **kwargs)
94
94
 
95
- File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\framework\tensor_util.py", line 248, in _check_failed
95
+ File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 219, in _variable_v1_call
96
96
 
97
- raise ValueError(v)
97
+ shape=shape)
98
98
 
99
+ File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 197, in <lambda>
100
+
101
+ previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
102
+
103
+ File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variable_scope.py", line 2519, in default_variable_creator
104
+
105
+ shape=shape)
106
+
107
+ File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 262, in __call__
108
+
109
+ return super(VariableMetaclass, cls).__call__(*args, **kwargs)
110
+
111
+ File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 1688, in __init__
112
+
113
+ shape=shape)
114
+
115
+ File "C:\Anaconda3\Lib\site-packages\tensorflow_core\python\ops\variables.py", line 1853, in _init_from_args
116
+
99
- ValueError: None
117
+ self._initial_value)
118
+
119
+ ValueError: initial_value must have a shape specified: Tensor("zeros:0", shape=(?, 2), dtype=float32)
100
120
 
101
121
  ```
102
122