【问题标题】:How to use backend on google colab如何在 google colab 上使用后端
【发布时间】:2018-07-12 18:39:17
【问题描述】:

Google Colab 是一个非常适合进行编码的地方。但是有一些问题我被窃听了。

我正在尝试使用 keras 后端和我找到的以下代码在经过训练的神经网络中输出中间结果,

from keras import backend as K

inp = model.input                                           # input placeholder
outputs = [layer.output for layer in model.layers]          # all layer outputs
functors = [K.function([inp]+ [K.learning_phase()], [out]) for out in outputs]  # evaluation functions

# Testing
test = np.random.random(input_shape)[np.newaxis,...]
layer_outs = [func([test, 1.]) for func in functors]
print layer_outs

它很好地弥补了这些功能。但是,当调用该函数时,它会报告以下错误,

FailedPreconditionErrorTraceback (most recent call last)
<ipython-input-18-f0000c1b16a6> in <module>()
----> 1 layer_outs = [func([X_test]) for func in functors]

/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc in __call__(self, inputs)
   2480         session = get_session()
   2481         updated = session.run(fetches=fetches, feed_dict=feed_dict,
-> 2482                               **self.session_kwargs)
   2483         return updated[:len(self.outputs)]
   2484 

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
    898     try:
    899       result = self._run(None, fetches, feed_dict, options_ptr,
--> 900                          run_metadata_ptr)
    901       if run_metadata:
    902         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1133     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1134       results = self._do_run(handle, final_targets, final_fetches,
-> 1135                              feed_dict_tensor, options, run_metadata)
   1136     else:
   1137       results = []

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1314     if handle is None:
   1315       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1316                            run_metadata)
   1317     else:
   1318       return self._do_call(_prun_fn, handle, feeds, fetches)

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args)
   1333         except KeyError:
   1334           pass
-> 1335       raise type(e)(node_def, op, message)
   1336 
   1337   def _extend_graph(self):

FailedPreconditionError: Error while reading resource variable dense_1/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/dense_1/kernel)
     [[Node: dense_1/MatMul/ReadVariableOp = ReadVariableOp[dtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](dense_1/kernel)]]
     [[Node: dense_1/Relu/_3 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_14_dense_1/Relu", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

我的神经网络似乎保存在本地,因此无法从 Google Colab 上的远程后端调用变量。我不确定是不是这样,我该如何解决这个问题。

代码在我的 Mac 上运行良好。但设法让它在 Google Colab 上运行似乎更令人愉快。

【问题讨论】:

    标签: keras localhost google-colaboratory


    【解决方案1】:

    我相信有许多方法可能对您有用。我发现最好从 colab 安装我的谷歌驱动器。这样就很容易加载和保存文件。

    try:
        from google.colab import drive
    except ModuleNotFoundError as colab_not_found:
        raise ModuleNotFoundError('Only run this cell on google colab!') from colab_not_found
    
    # This will prompt for authorization.
    drive.mount('/content/drive')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-24
      • 2019-04-25
      • 2020-10-30
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 2019-03-21
      • 2021-12-15
      相关资源
      最近更新 更多