doc-ai / tensorio-ios

Tensor/IO for iOS, with support for on-device inference and training with TensorFlow and TensorFlow Lite
Other
31 stars 10 forks source link

Support array placeholders and send placeholders into model from task #174

Closed phildow closed 5 years ago

phildow commented 5 years ago

Addresses https://github.com/doc-ai/tensorio-ios/issues/157 Addresses https://github.com/doc-ai/tensorio-ios/issues/94

Add placeholder in python. Note must use a vector and reshape to scalar until https://github.com/doc-ai/tensorio-ios/issues/32 is resolved.

# placeholders: must not be scalars until tensor/io issued #32 is resolved, so reshape

adam_learning_rate = tf.placeholder_with_default([1e-4], shape=(1), name="placeholder_adam_learning_rate")
adam_learning_rate = tf.reshape(adam_learning_rate, ())

optimizer = tf.train.AdamOptimizer(learning_rate=adam_learning_rate)

Export for training as usual and in model.json note placeholder:

{
  "placeholders": [
    {
      "name": "placeholder_adam_learning_rate",
      "type": "array",
      "dtype": "float32",
      "shape": [1]
    }
  ],
}

And in task.json:

{
  "taskParameters": {
  "numEpochs": 10,
  "batchSize": 2,
  "shuffle": false,
  "placeholders": [
    {
      "name": "placeholder_adam_learning_rate",
      "type": "array",
      "dtype": "float32",
      "shape": [1],
      "value": [0.001]
    }
  ]
}

Note that the description of the placeholder in task.json must match the description of the placeholder in model.json, except for the addition of the value field, otherwise training will fail and raise an error.