Переменная не была создана в области стратегии распространения - PullRequest
0 голосов
/ 20 марта 2020

Как скомпилировать модель для обучения TPU с помощью экстрактора функций tf.hub?

import tensorflow_hub as hub


with strategy.scope():
    inp=Input(shape=DIMS)
    base_feat = hub.KerasLayer("https://tfhub.dev/tensorflow/efficientnet/lite0/feature-vector/1",input_shape=(512,512,3))(inp)
    out=Dense(4,activation='sigmoid')(base_feat)
    model=Model(inp,out)
    model.compile(loss='binary_crossentropy',optimizer='Adam',metrics=['acc'])

ошибка:

    ValueError                                Traceback (most recent call last)
<ipython-input-246-f0b8d9e32b32> in <module>()
      9     out=Dense(4,activation='sigmoid')(base_feat)
     10     model=Model(inp,out)
---> 11     model.compile(loss='binary_crossentropy',optimizer='Adam',metrics=['acc'])

1 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, distribute, **kwargs)
    469                 'with strategy.scope():\n'
    470                 '  model=_create_model()\n'
--> 471                 '  model.compile(...)'% (v, strategy))
    472 
    473   @trackable.no_automatic_dependency_tracking

ValueError: Variable (<tf.Variable 'efficientnet-lite0/stem/conv2d/kernel:0' shape=(3, 3, 3, 32) dtype=float32>) was not created in the distribution strategy scope of (<tensorflow.python.distribute.tpu_strategy.TPUStrategy object at 0x7fb6a9aa9358>). It is most likely due to not all layers or the model or optimizer being created outside the distribution strategy scope. Try to make sure your code looks similar to the following.
with strategy.scope():
  model=_create_model()
  model.compile(...)

Я пытался использовать входные данные из base_feat:

импортировать тензор потока_хаб как хаб

with strategy.scope():
    base_feat = hub.KerasLayer("https://tfhub.dev/tensorflow/efficientnet/lite0/feature-vector/1",input_shape=(512,512,3))
    out=Dense(4,activation='sigmoid')(base_feat)
    model=Model(base_feat.input,out)
    #model.build((512,512,3))
    model.compile(loss='binary_crossentropy',optimizer='Adam',metrics=['acc'])

, но получил следующую ошибку:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-254-8984ca67cd6f> in <module>()
      5 with strategy.scope():
      6     base_feat = hub.KerasLayer("https://tfhub.dev/tensorflow/efficientnet/lite0/feature-vector/1",input_shape=(512,512,3))
----> 7     out=Dense(4,activation='sigmoid')(base_feat)
      8     model=Model(base_feat.input,out)
      9     model.build((512,512,3))

2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    161         spec.min_ndim is not None or
    162         spec.max_ndim is not None):
--> 163       if x.shape.ndims is None:
    164         raise ValueError('Input ' + str(input_index) + ' of layer ' +
    165                          layer_name + ' is incompatible with the layer: '

AttributeError: 'KerasLayer' object has no attribute 'shape'

Также я попытался использовать последовательные керасы, тоже не получилось.

Спасибо!

...