DeepChem GraphConvodel (GNN) 训练 TypeError

时间:2021-04-29 10:49:46

标签: machine-learning neural-network artificial-intelligence classification prediction

我是 GNN 的初学者,我正在尝试使用 DeepChem 的 Tox21 数据集预测药物毒性的代码。它是一个包含 12,000 个化合物的训练集和 650 个化合物的测试集的数据集。我需要帮助调试和纠正这个错误:“TypeError: 'NoneType' object is not subscriptable”,我在最后得到了。

这是代码片段:

model = GraphConvModel(len(tox21_tasks), 
                   batch_size=32, 
                   mode='classification')

print("Fitting the model")

model.fit(train_dataset, nb_epoch=10)

这是我的错误:

    TypeError                                 Traceback (most recent call last)
<ipython-input-5-8088249b7fd6> in <module>
      4                        mode='classification')
      5 print("Fitting the model")
----> 6 model.fit(train_dataset, nb_epoch=10)

~\anaconda3\lib\site-packages\deepchem\models\keras_model.py in fit(self, dataset, nb_epoch, max_checkpoints_to_keep, checkpoint_interval, deterministic, restore, variables, loss, callbacks, all_losses)
    322             dataset, epochs=nb_epoch,
    323             deterministic=deterministic), max_checkpoints_to_keep,
--> 324         checkpoint_interval, restore, variables, loss, callbacks, all_losses)
    325 
    326   def fit_generator(self,

~\anaconda3\lib\site-packages\deepchem\models\keras_model.py in fit_generator(self, generator, max_checkpoints_to_keep, checkpoint_interval, restore, variables, loss, callbacks, all_losses)
    407         inputs = inputs[0]
    408 
--> 409       batch_loss = apply_gradient_for_batch(inputs, labels, weights, loss)
    410       current_step = self._global_step.numpy()
    411 

~\anaconda3\lib\site-packages\tensorflow_core\python\eager\def_function.py in __call__(self, *args, **kwds)
    455 
    456     tracing_count = self._get_tracing_count()
--> 457     result = self._call(*args, **kwds)
    458     if tracing_count == self._get_tracing_count():
    459       self._call_counter.called_without_tracing()

~\anaconda3\lib\site-packages\tensorflow_core\python\eager\def_function.py in _call(self, *args, **kwds)
    501       # This is the first call of __call__, so we have to initialize.
    502       initializer_map = object_identity.ObjectIdentityDictionary()
--> 503       self._initialize(args, kwds, add_initializers_to=initializer_map)
    504     finally:
    505       # At this point we know that the initialization is complete (or less

~\anaconda3\lib\site-packages\tensorflow_core\python\eager\def_function.py in _initialize(self, args, kwds, add_initializers_to)
    406     self._concrete_stateful_fn = (
    407         self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
--> 408             *args, **kwds))
    409 
    410     def invalid_creator_scope(*unused_args, **unused_kwds):

~\anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
   1846     if self.input_signature:
   1847       args, kwargs = None, None
-> 1848     graph_function, _, _ = self._maybe_define_function(args, kwargs)
   1849     return graph_function
   1850 

~\anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py in _maybe_define_function(self, args, kwargs)
   2148         graph_function = self._function_cache.primary.get(cache_key, None)
   2149         if graph_function is None:
-> 2150           graph_function = self._create_graph_function(args, kwargs)
   2151           self._function_cache.primary[cache_key] = graph_function
   2152         return graph_function, args, kwargs

~\anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
   2039             arg_names=arg_names,
   2040             override_flat_arg_shapes=override_flat_arg_shapes,
-> 2041             capture_by_value=self._capture_by_value),
   2042         self._function_attributes,
   2043         # Tell the ConcreteFunction to clean up its graph once it goes out of

~\anaconda3\lib\site-packages\tensorflow_core\python\framework\func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
    913                                           converted_func)
    914 
--> 915       func_outputs = python_func(*func_args, **func_kwargs)
    916 
    917       # invariant: `func_outputs` contains only Tensors, CompositeTensors,

~\anaconda3\lib\site-packages\tensorflow_core\python\eager\def_function.py in wrapped_fn(*args, **kwds)
    356         # __wrapped__ allows AutoGraph to swap in a converted function. We give
    357         # the function a weak reference to itself to avoid a reference cycle.
--> 358         return weak_wrapped_fn().__wrapped__(*args, **kwds)
    359     weak_wrapped_fn = weakref.ref(wrapped_fn)
    360 

~\anaconda3\lib\site-packages\tensorflow_core\python\framework\func_graph.py in wrapper(*args, **kwargs)
    903           except Exception as e:  # pylint:disable=broad-except
    904             if hasattr(e, "ag_error_metadata"):
--> 905               raise e.ag_error_metadata.to_exception(e)
    906             else:
    907               raise

TypeError: in converted code:
    relative to C:\Users\Madiha\anaconda3\lib\site-packages:

    deepchem\models\keras_model.py:474 apply_gradient_for_batch  *
        grads = tape.gradient(batch_loss, vars)
    tensorflow_core\python\eager\backprop.py:1014 gradient
        unconnected_gradients=unconnected_gradients)
    tensorflow_core\python\eager\imperative_grad.py:76 imperative_grad
        compat.as_str(unconnected_gradients.value))
    tensorflow_core\python\eager\backprop.py:138 _gradient_function
        return grad_fn(mock_op, *out_grads)
    tensorflow_core\python\ops\math_grad.py:455 _UnsortedSegmentMaxGrad
        return _UnsortedSegmentMinOrMaxGrad(op, grad)
    tensorflow_core\python\ops\math_grad.py:432 _UnsortedSegmentMinOrMaxGrad
        _GatherDropNegatives(op.outputs[0], op.inputs[1])

    TypeError: 'NoneType' object is not subscriptable

1 个答案:

答案 0 :(得分:0)

作为建议,请查看 DeepChem 网站上的一些示例。这是一个可以工作的代码:

tasks, datasets, transformers = dc.molnet.load_tox21(featurizer='GraphConv')
train_dataset, valid_dataset, test_dataset = datasets

model = dc.models.GraphConvModel(len(tasks), 
                   batch_size=32, 
                   mode='classification')

print("Fitting the model")

model.fit(train_dataset)

希望对你有用!