Exemplo n.º 1
0
    def testRNN(self):
        random.seed(42)
        data = np.array(list([[2, 1, 2, 2, 3], [2, 2, 3, 4, 5],
                              [3, 3, 1, 2, 1], [2, 4, 5, 4, 1]]),
                        dtype=np.float32)
        # labels for classification
        labels = np.array(list([1, 0, 1, 0]), dtype=np.float32)
        # targets for regression
        targets = np.array(list([10, 16, 10, 16]), dtype=np.float32)
        test_data = np.array(list([[1, 3, 3, 2, 1], [2, 3, 4, 5, 6]]),
                             dtype=np.float32)

        def _input_fn(X):
            # pylint: disable=invalid-name
            return tf.split(1, 5, X)

        # Classification
        classifier = learn.TensorFlowRNNClassifier(rnn_size=2,
                                                   cell_type="lstm",
                                                   n_classes=2,
                                                   input_op_fn=_input_fn)
        classifier.fit(data, labels)
        # pylint: disable=pointless-statement
        classifier.weights_
        classifier.bias_
        # pylint: enable=pointless-statement
        predictions = classifier.predict(test_data)
        self.assertAllClose(predictions, np.array([1, 0]))

        classifier = learn.TensorFlowRNNClassifier(rnn_size=2,
                                                   cell_type="rnn",
                                                   n_classes=2,
                                                   input_op_fn=_input_fn,
                                                   num_layers=2)
        classifier.fit(data, labels)
        classifier = learn.TensorFlowRNNClassifier(
            rnn_size=2,
            cell_type="invalid_cell_type",
            n_classes=2,
            input_op_fn=_input_fn,
            num_layers=2)
        with self.assertRaises(ValueError):
            classifier.fit(data, labels)

        # Regression
        regressor = learn.TensorFlowRNNRegressor(rnn_size=2,
                                                 cell_type="gru",
                                                 input_op_fn=_input_fn)
        regressor.fit(data, targets)
        # pylint: disable=pointless-statement
        regressor.weights_
        regressor.bias_
        # pylint: enable=pointless-statement
        predictions = regressor.predict(test_data)
Exemplo n.º 2
0
    def testRNN(self):
        random.seed(42)
        import numpy as np
        data = np.array(list([[2, 1, 2, 2, 3], [2, 2, 3, 4, 5],
                              [3, 3, 1, 2, 1], [2, 4, 5, 4, 1]]),
                        dtype=np.float32)
        # labels for classification
        labels = np.array(list([1, 0, 1, 0]), dtype=np.float32)
        # targets for regression
        targets = np.array(list([10, 16, 10, 16]), dtype=np.float32)
        test_data = np.array(list([[1, 3, 3, 2, 1], [2, 3, 4, 5, 6]]))

        def input_fn(X):
            return tf.split(1, 5, X)

        # Classification
        classifier = learn.TensorFlowRNNClassifier(rnn_size=2,
                                                   cell_type='lstm',
                                                   n_classes=2,
                                                   input_op_fn=input_fn)
        classifier.fit(data, labels)
        classifier.weights_
        classifier.bias_
        predictions = classifier.predict(test_data)
        self.assertAllClose(predictions, np.array([1, 0]))

        classifier = learn.TensorFlowRNNClassifier(rnn_size=2,
                                                   cell_type='rnn',
                                                   n_classes=2,
                                                   input_op_fn=input_fn,
                                                   num_layers=2)
        classifier.fit(data, labels)
        classifier = learn.TensorFlowRNNClassifier(
            rnn_size=2,
            cell_type='invalid_cell_type',
            n_classes=2,
            input_op_fn=input_fn,
            num_layers=2)
        with self.assertRaises(ValueError):
            classifier.fit(data, labels)

        # Regression
        regressor = learn.TensorFlowRNNRegressor(rnn_size=2,
                                                 cell_type='gru',
                                                 input_op_fn=input_fn)
        regressor.fit(data, targets)
        regressor.weights_
        regressor.bias_
        predictions = regressor.predict(test_data)
Exemplo n.º 3
0
    def testBidirectionalRNN(self):
        random.seed(42)
        import numpy as np
        data = np.array(list([[2, 1, 2, 2, 3], [2, 2, 3, 4, 5],
                              [3, 3, 1, 2, 1], [2, 4, 5, 4, 1]]),
                        dtype=np.float32)
        labels = np.array(list([1, 0, 1, 0]), dtype=np.float32)

        def input_fn(X):
            return tf.split(1, 5, X)

        # Classification
        classifier = learn.TensorFlowRNNClassifier(rnn_size=2,
                                                   cell_type='lstm',
                                                   n_classes=2,
                                                   input_op_fn=input_fn,
                                                   bidirectional=True)
        classifier.fit(data, labels)
        predictions = classifier.predict(
            np.array(list([[1, 3, 3, 2, 1], [2, 3, 4, 5, 6]])))
        self.assertAllClose(predictions, np.array([1, 0]))
Exemplo n.º 4
0
    def testBidirectionalRNN(self):
        random.seed(42)
        data = np.array(list([[2, 1, 2, 2, 3], [2, 2, 3, 4, 5],
                              [3, 3, 1, 2, 1], [2, 4, 5, 4, 1]]),
                        dtype=np.float32)
        labels = np.array(list([1, 0, 1, 0]), dtype=np.float32)

        def _input_fn(X):
            # pylint: disable=invalid-name
            return tf.split(1, 5, X)

        # Classification
        classifier = learn.TensorFlowRNNClassifier(rnn_size=2,
                                                   cell_type="lstm",
                                                   n_classes=2,
                                                   input_op_fn=_input_fn,
                                                   bidirectional=True)
        classifier.fit(data, labels)
        test_data = np.array(list([[1, 3, 3, 2, 1], [2, 3, 4, 5, 6]]),
                             dtype=np.float32)
        predictions = classifier.predict(test_data)
        self.assertAllClose(predictions, np.array([1, 0]))