MCPcopy Create free account
hub / github.com/pytorch/pytorch / test_initializer

Method test_initializer

caffe2/python/onnx/tests/c2_ref_test.py:140–172  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

138 self.assertSameOutputs(c2_outputs, onnx_outputs)
139
140 def test_initializer(self):
141 X = np.array([[1, 2], [3, 4]]).astype(np.float32)
142 Y = np.array([[1, 2], [3, 4]]).astype(np.float32)
143 weight = np.array([[1, 0], [0, 1]])
144 graph_def = make_graph(
145 [make_node("Add", ["X", "Y"], ["Z0"]),
146 make_node("Cast", ["Z0"], ["Z"], to=onnx.TensorProto.FLOAT),
147 make_node("Mul", ["Z", "weight"], ["W0"]),
148 make_node("Tanh", ["W0"], ["W1"]),
149 make_node("Sigmoid", ["W1"], ["W2"]),
150 make_node("Scale", ["W2"], ["W3"], scale=-1.0)],
151 name="test_initializer",
152 inputs=[
153 make_tensor_value_info("X", onnx.TensorProto.FLOAT, (2, 2)),
154 make_tensor_value_info("Y", onnx.TensorProto.FLOAT, (2, 2)),
155 make_tensor_value_info("weight", onnx.TensorProto.FLOAT, (2, 2)),
156 ],
157 outputs=[
158 make_tensor_value_info("W3", onnx.TensorProto.FLOAT, (2, 2))
159 ],
160 initializer=[make_tensor("weight",
161 onnx.TensorProto.FLOAT,
162 [2, 2],
163 weight.flatten().astype(float))]
164 )
165
166 def sigmoid(x):
167 return 1 / (1 + np.exp(-x))
168
169 W_ref = -sigmoid(np.tanh((X + Y) * weight))
170 c2_rep = c2.prepare(make_model(graph_def, producer_name='caffe2-ref-test'))
171 output = c2_rep.run({"X": X, "Y": Y})
172 np.testing.assert_almost_equal(output["W3"], W_ref)
173
174 def test_reducemean(self):
175 X = np.random.randn(4, 6, 10, 5, 3).astype(np.float32)

Callers

nothing calls this directly

Calls 8

make_tensorFunction · 0.85
make_modelFunction · 0.85
astypeMethod · 0.80
sigmoidFunction · 0.50
flattenMethod · 0.45
tanhMethod · 0.45
prepareMethod · 0.45
runMethod · 0.45

Tested by

no test coverage detected