Exemplo n.º 1
0
    def test_tf_load(self):
        linear = Linear(10, 2)()
        sigmoid = Sigmoid()(linear)
        softmax = SoftMax().set_name("output")(sigmoid)
        model = BModel(linear, softmax)
        input = np.random.random((4, 10))

        tmp_path = create_tmp_path() + "/model.pb"

        model.save_tensorflow([("input", [4, 10])], tmp_path)

        model_reloaded = Net.load_tf(tmp_path, ["input"], ["output"])
        expected_output = model.forward(input)
        output = model_reloaded.forward(input)
        self.assert_allclose(output, expected_output)
Exemplo n.º 2
0
def main():
    tf.set_random_seed(1234)
    input = tf.placeholder(tf.float32, [None, 5])
    weight = tf.Variable(tf.random_uniform([5, 10]))
    bias = tf.Variable(tf.random_uniform([10]))
    middle = tf.nn.bias_add(tf.matmul(input, weight), bias)
    output = tf.nn.tanh(middle)

    tensor = np.random.rand(5, 5)
    # construct BigDL model and get the result form
    bigdl_model = Model(input, output, model_type="tensorflow")
    bigdl_result = bigdl_model.forward(tensor)

    # get result from tensorflow and compare
    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)
        tensorflow_result = sess.run(output, {input: tensor})

        print("Tensorflow forward result is " + str(tensorflow_result))
        print("BigDL forward result is " + str(bigdl_result))

        np.testing.assert_almost_equal(tensorflow_result, bigdl_result, 6)
        print("The results are almost equal in 6 decimals")