Пример #1
0
    def testCompositeTypeSpecArgWithoutDtype(self):
        for assign_variant_dtype in [False, True]:
            # Create a Keras Input
            spec = TwoTensorsSpecNoOneDtype(
                (1, 2, 3),
                dtypes.float32, (1, 2, 3),
                dtypes.int64,
                assign_variant_dtype=assign_variant_dtype)
            x = input_layer_lib.Input(type_spec=spec)

            def lambda_fn(tensors):
                return (math_ops.cast(tensors.x, dtypes.float64) +
                        math_ops.cast(tensors.y, dtypes.float64))

            # Verify you can construct and use a model w/ this input
            model = functional.Functional(x, core.Lambda(lambda_fn)(x))

            # And that the model works
            two_tensors = TwoTensors(
                array_ops.ones((1, 2, 3)) * 2.0, array_ops.ones(1, 2, 3))
            self.assertAllEqual(model(two_tensors), lambda_fn(two_tensors))

            # Test serialization / deserialization
            model = functional.Functional.from_config(model.get_config())
            self.assertAllEqual(model(two_tensors), lambda_fn(two_tensors))
            model = model_config.model_from_json(model.to_json())
            self.assertAllEqual(model(two_tensors), lambda_fn(two_tensors))
Пример #2
0
  def test_saving_with_dense_features(self):
    cols = [
        feature_column_lib.numeric_column('a'),
        feature_column_lib.indicator_column(
            feature_column_lib.categorical_column_with_vocabulary_list(
                'b', ['one', 'two']))
    ]
    input_layers = {
        'a': keras.layers.Input(shape=(1,), name='a'),
        'b': keras.layers.Input(shape=(1,), name='b', dtype='string')
    }

    fc_layer = dense_features.DenseFeatures(cols)(input_layers)
    output = keras.layers.Dense(10)(fc_layer)

    model = keras.models.Model(input_layers, output)

    model.compile(
        loss=keras.losses.MSE,
        optimizer='rmsprop',
        metrics=[keras.metrics.categorical_accuracy])

    config = model.to_json()
    loaded_model = model_config.model_from_json(config)

    inputs_a = np.arange(10).reshape(10, 1)
    inputs_b = np.arange(10).reshape(10, 1).astype('str')

    with self.cached_session():
      # Initialize tables for V1 lookup.
      if not context.executing_eagerly():
        self.evaluate(lookup_ops.tables_initializer())

      self.assertLen(loaded_model.predict({'a': inputs_a, 'b': inputs_b}), 10)
Пример #3
0
    def CNN_test_model(self, X_test, verbose, y_test):
        #X_test = X_test.reshape((1, self.timestep, 4 ))

        # Loads saved model so retraining isn't needed
        json_file = open('saved_models/CNN.json', 'r')
        loaded_model_json = json_file.read()
        json_file.close()
        model = model_from_json(loaded_model_json)
        model.load_weights('saved_models/CNN.h5')

        print(X_test)
        print(X_test.shape)

        # Predicts on trained model
        yhat = model.predict(X_test, verbose=verbose)

        # Prints the results
        yhat = np.concatenate((yhat), axis=1)
        print('Test:', X_test)
        print('Next Actual:\n', y_test)
        print('Next Predicted:\n', yhat)

        # columns = ['Open', 'High', 'Low', 'Close']
        # files = ['open.csv', 'high.csv', 'low.csv', 'close.csv']
        # for i in range(0,4):
        # with open("../Testing/" + files[i], "a+", newline='') as csvfile:
        #     csvwriter = csv.writer(csvfile)
        #     #csvwriter.writerow(['Column', 'Mean absolute error', 'Mean squared error', 'Explain variance score', 'R2 score', kval])
        #     #for i in range(0, 4):
        #     csvwriter.writerow([mae, mse, r2])

        return yhat
Пример #4
0
    def test_saving_with_sequence_features(self):
        cols = [
            feature_column_lib.sequence_numeric_column('a'),
            feature_column_lib.indicator_column(
                feature_column_lib.
                sequence_categorical_column_with_vocabulary_list(
                    'b', ['one', 'two']))
        ]
        input_layers = {
            'a':
            keras.layers.Input(shape=(None, 1), sparse=True, name='a'),
            'b':
            keras.layers.Input(shape=(None, 1),
                               sparse=True,
                               name='b',
                               dtype='string')
        }

        fc_layer, _ = feature_column_lib.SequenceFeatures(cols)(input_layers)
        # TODO(tibell): Figure out the right dtype and apply masking.
        # sequence_length_mask = array_ops.sequence_mask(sequence_length)
        # x = keras.layers.GRU(32)(fc_layer, mask=sequence_length_mask)
        x = keras.layers.GRU(32)(fc_layer)
        output = keras.layers.Dense(10)(x)

        model = keras.models.Model(input_layers, output)

        model.compile(loss=keras.losses.MSE,
                      optimizer='rmsprop',
                      metrics=[keras.metrics.categorical_accuracy])

        config = model.to_json()
        loaded_model = model_config.model_from_json(config)

        batch_size = 10
        timesteps = 1

        values_a = np.arange(10, dtype=np.float32)
        indices_a = np.zeros((10, 3), dtype=np.int64)
        indices_a[:, 0] = np.arange(10)
        inputs_a = sparse_tensor.SparseTensor(indices_a, values_a,
                                              (batch_size, timesteps, 1))

        values_b = np.zeros(10, dtype=np.str)
        indices_b = np.zeros((10, 3), dtype=np.int64)
        indices_b[:, 0] = np.arange(10)
        inputs_b = sparse_tensor.SparseTensor(indices_b, values_b,
                                              (batch_size, timesteps, 1))

        with self.cached_session():
            # Initialize tables for V1 lookup.
            if not context.executing_eagerly():
                self.evaluate(lookup_ops.tables_initializer())

            self.assertLen(
                loaded_model.predict({
                    'a': inputs_a,
                    'b': inputs_b
                }, steps=1), batch_size)
 def test_json_serialization(self):
   inputs = keras.Input(shape=(4,), dtype='uint8')
   outputs = math_ops.cast(inputs, 'float32') / 4.
   model = model_config.model_from_json(keras.Model(inputs, outputs).to_json())
   self.assertAllEqual(
       self.evaluate(model(np.array([0, 64, 128, 192], np.uint8))),
       [0., 16., 32., 48.])
   model.summary()
Пример #6
0
def load_from_saved_model(saved_model_path, custom_objects=None):
  """Loads a keras Model from a SavedModel created by `export_saved_model()`.

  This function reinstantiates model state by:
  1) loading model topology from json (this will eventually come
     from metagraph).
  2) loading model weights from checkpoint.

  Example:

  ```python
  import tensorflow as tf

  # Create a tf.keras model.
  model = tf.keras.Sequential()
  model.add(tf.keras.layers.Dense(1, input_shape=[10]))
  model.summary()

  # Save the tf.keras model in the SavedModel format.
  path = '/tmp/simple_keras_model'
  tf.keras.experimental.export_saved_model(model, path)

  # Load the saved keras model back.
  new_model = tf.keras.experimental.load_from_saved_model(path)
  new_model.summary()
  ```

  Args:
    saved_model_path: a string specifying the path to an existing SavedModel.
    custom_objects: Optional dictionary mapping names
        (strings) to custom classes or functions to be
        considered during deserialization.

  Returns:
    a keras.Model instance.
  """
  warnings.warn('`tf.keras.experimental.load_from_saved_model` is deprecated'
                'and will be removed in a future version. '
                'Please switch to `tf.keras.models.load_model`.')
  # restore model topology from json string
  model_json_filepath = os.path.join(
      compat.as_bytes(saved_model_path),
      compat.as_bytes(constants.ASSETS_DIRECTORY),
      compat.as_bytes(constants.SAVED_MODEL_FILENAME_JSON))
  with gfile.Open(model_json_filepath, 'r') as f:
    model_json = f.read()
  model = model_config.model_from_json(
      model_json, custom_objects=custom_objects)

  # restore model weights
  checkpoint_prefix = os.path.join(
      compat.as_text(saved_model_path),
      compat.as_text(constants.VARIABLES_DIRECTORY),
      compat.as_text(constants.VARIABLES_FILENAME))
  model.load_weights(checkpoint_prefix)
  return model
Пример #7
0
    def test_saving_with_dense_features(self):
        cols = [feature_column_v2.numeric_column('a')]
        input_layer = keras.layers.Input(shape=(1, ), name='a')
        fc_layer = feature_column_v2.DenseFeatures(cols)({'a': input_layer})
        output = keras.layers.Dense(10)(fc_layer)

        model = keras.models.Model(input_layer, output)

        model.compile(loss=keras.losses.MSE,
                      optimizer=keras.optimizers.RMSprop(lr=0.0001),
                      metrics=[keras.metrics.categorical_accuracy])

        config = model.to_json()
        loaded_model = model_config.model_from_json(config)

        inputs = np.arange(10).reshape(10, 1)
        self.assertLen(loaded_model.predict({'a': inputs}), 10)
    def testCompositeTypeSpecArg(self):
        # Create a Keras Input
        rt = ragged_tensor.RaggedTensor.from_row_splits(
            values=[3, 1, 4, 1, 5, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
        x = input_layer_lib.Input(type_spec=rt._type_spec)

        # Verify you can construct and use a model w/ this input
        model = functional.Functional(x, x * 2)

        # And that the model works
        rt = ragged_tensor.RaggedTensor.from_row_splits(
            values=[3, 21, 4, 1, 53, 9, 2, 6], row_splits=[0, 4, 4, 7, 8, 8])
        self.assertAllEqual(model(rt), rt * 2)

        # Test serialization / deserialization
        model = functional.Functional.from_config(model.get_config())
        self.assertAllEqual(model(rt), rt * 2)
        model = model_config.model_from_json(model.to_json())
        self.assertAllEqual(model(rt), rt * 2)
    def testTypeSpecArg(self):
        # Create a Keras Input
        x = input_layer_lib.Input(
            type_spec=tensor_spec.TensorSpec((7, 32), dtypes.float32))
        self.assertAllEqual(x.shape.as_list(), [7, 32])

        # Verify you can construct and use a model w/ this input
        model = functional.Functional(x, x * 2.0)
        self.assertAllEqual(model(array_ops.ones(x.shape)),
                            array_ops.ones(x.shape) * 2.0)

        # Test serialization / deserialization
        model = functional.Functional.from_config(model.get_config())
        self.assertAllEqual(model(array_ops.ones(x.shape)),
                            array_ops.ones(x.shape) * 2.0)

        model = model_config.model_from_json(model.to_json())
        self.assertAllEqual(model(array_ops.ones(x.shape)),
                            array_ops.ones(x.shape) * 2.0)
Пример #10
0
    def MLP_test_model(X_test, verbose, y_test):

        # Loads saved model so retraining isn't needed
        json_file = open('saved_models/MLP.json', 'r')
        loaded_model_json = json_file.read()
        json_file.close()
        model = model_from_json(loaded_model_json)
        model.load_weights('saved_models/MLP.h5')

        n_input = X_test.shape[1] * X_test.shape[2]
        X_test = X_test.reshape((X_test.shape[0], n_input))
        print(type(X_test))
        yhat = model.predict(X_test, verbose=verbose)

        yhat = np.concatenate((yhat), axis=1)
        print('Test:\n', X_test)

        close = X_test[:, -1]
        low = X_test[:, -2]
        high = X_test[:, -3]
        opens = X_test[:, -4]

        final_cols = np.column_stack((opens, high, low, close))

        print('Current:\n Open   High    Low    Close\n', final_cols)

        print('Next:\n', y_test)
        print('Predicted:\n', yhat)

        columns = ['Open', 'High', 'Low', 'Close']
        files = ['open.csv', 'high.csv', 'low.csv', 'close.csv']
        for i in range(0, 4):
            mae = round(sm.mean_absolute_error(y_test[:, i], yhat[:, i]), 20)
            mse = round(sm.mean_squared_error(y_test[:, i], yhat[:, i]), 20)
            r2 = round(sm.r2_score(y_test[:, i], yhat[:, i]), 20)
        #     with open('C:/Users/Ryan Easter/OneDrive - University of Lincoln/University/Year 4 (Final)/Project/Artefact/Project-Soros/Testing/' + files[i], 'a+', newline='') as csvfile:
        #         csvwriter = csv.writer(csvfile)
        #         #csvwriter.writerow(['Column', 'Mean absolute error', 'Mean squared error', 'Explain variance score', 'R2 score', kval])
        #         #for i in range(0, 4):
        #         csvwriter.writerow([mae, mse, r2])

        return yhat, final_cols