jianhuupenn / ItClust

Iterative transfer learning with neural network improves clustering and cell type classification in single-cell RNA-seq analysis
51 stars 8 forks source link

TypeError: add_weight() got multiple values for argument 'name' #2

Closed cartal closed 4 years ago

cartal commented 4 years ago

Hi,

Thanks for making this code available.

I'm having an issue with my training. I have my reference dataset, and I have split like this:

lymphoid_train = lymphoid[msk]
lymphoid_train.shape

lymphoid_test = lymphoid[~msk]
lymphoid_test.shape

And then I train my model like this:

clf = ic.transfer_learning_clf()
clf.fit(lymphoid_train, lymphoid_test)

Then it runs and everything seems to go well, until it reaches this point The shape of cluster_center is (5, 32):

/opt/conda/lib/python3.7/site-packages/scanpy/preprocessing/_simple.py:297: DeprecationWarning: Use is_view instead of isview, isview will be removed in the future.
  if isinstance(data, AnnData) and data.isview:
the var_names of adata.raw: adata.raw.var_names.is_unique=: True
/opt/conda/lib/python3.7/site-packages/scanpy/preprocessing/_simple.py:297: DeprecationWarning: Use is_view instead of isview, isview will be removed in the future.
  if isinstance(data, AnnData) and data.isview:
/opt/conda/lib/python3.7/site-packages/scanpy/_utils.py:341: DeprecationWarning: Use is_view instead of isview, isview will be removed in the future.
  if adata.isview:
/opt/conda/lib/python3.7/site-packages/scanpy/preprocessing/_simple.py:297: DeprecationWarning: Use is_view instead of isview, isview will be removed in the future.
  if isinstance(data, AnnData) and data.isview:
the var_names of adata.raw: adata.raw.var_names.is_unique=: True
/opt/conda/lib/python3.7/site-packages/scanpy/preprocessing/_simple.py:297: DeprecationWarning: Use is_view instead of isview, isview will be removed in the future.
  if isinstance(data, AnnData) and data.isview:
/opt/conda/lib/python3.7/site-packages/scanpy/_utils.py:341: DeprecationWarning: Use is_view instead of isview, isview will be removed in the future.
  if adata.isview:
The number of training celltypes is:  6
Training the source network
The layer numbers are[128, 32]
The shape of xtrain is:48411:2000
The shape of xtest is:12201:2000
Doing DEC: pretrain
...Pretraining...
Doing SAE: pretrain_stacks
Pretraining the 1th layer...
learning rate = 0.1
learning rate = 0.01
learning rate = 0.001
The 1th layer has been pretrained.
Pretraining the 2th layer...
learning rate = 0.1
learning rate = 0.01
learning rate = 0.001
The 2th layer has been pretrained.
Doing SAE: pretrain_autoencoders
Copying layer-wise pretrained weights to deep autoencoders
Fine-tuning autoencoder end-to-end
learning rate = 0.1
learning rate = 0.010000000000000002
learning rate = 0.001
learning rate = 0.0001
learning rate = 1e-05
learning rate = 1.0000000000000002e-06
Pretraining time:  485.230003118515
y known, initilize Cluster centroid using y
The shape of cluster_center is (5, 32)

Which gives the following error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-46-f8f8814c2c8e> in <module>
----> 1 clf.fit(lymphoid_train, lymphoid_test)

/opt/conda/lib/python3.7/site-packages/ItClust/ItClust.py in fit(***failed resolving arguments***)
    139         print(":".join(["The shape of xtest is",str(x_test.shape[0]),str(x_test.shape[1])]))
    140         assert x_train.shape[1]==x_test.shape[1]
--> 141         dec=DEC(dims=dims,y=y_train,x=x_train,alpha=alpha,init=self.init,pretrain_epochs=self.pretrain_epochs,actinlayer1="tanh",softmax=softmax)
    142         dec.compile(optimizer=SGD(lr=0.01,momentum=0.9))
    143         #print("dec.init_centroid",type(dec.init_centroid),dec.init_centroid)

/opt/conda/lib/python3.7/site-packages/ItClust/DEC.py in __init__(self, dims, x, y, alpha, init, n_clusters, louvain_resolution, n_neighbors, pretrain_epochs, ae_weights, actinlayer1, is_stacked, transfer_feature, model_weights, y_trans, softmax)
    115         self.pretrain_epochs=pretrain_epochs
    116         if  self.transfer_feature is None:
--> 117             self.pretrain(n_neighbors=n_neighbors,epochs=self.pretrain_epochs,n_clusters=n_clusters)
    118         else:
    119             self.pretrain_transfer(n_neighbors=n_neighbors,model_weights=self.model_weights,features=transfer_feature,epochs=self.pretrain_epochs,n_clusters=n_clusters,y_trans=self.y_trans)

/opt/conda/lib/python3.7/site-packages/ItClust/DEC.py in pretrain(self, optimizer, epochs, n_neighbors, batch_size, n_clusters)
    172             print("The shape of cluster_center is",cluster_centers.shape)
    173         if not self.softmax:       # Use dec method to do clustering
--> 174             clustering_layer = ClusteringLayer(self.n_clusters, name='clustering')(self.encoder.output)
    175         else:        # Use softmax to do clustering
    176             clustering_layer=Dense(self.n_clusters,kernel_initializer="glorot_uniform",name="clustering",activation='softmax')(self.encoder.output)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    895           # Build layer if applicable (if the `build` method has been
    896           # overridden).
--> 897           self._maybe_build(inputs)
    898           cast_inputs = self._maybe_cast_inputs(inputs)
    899 

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
   2414         # operations.
   2415         with tf_utils.maybe_init_scope(self):
-> 2416           self.build(input_shapes)  # pylint:disable=not-callable
   2417       # We must set also ensure that the layer is marked as built, and the build
   2418       # shape is stored since user defined build functions may not be calling

/opt/conda/lib/python3.7/site-packages/ItClust/DEC.py in build(self, input_shape)
     51         input_dim = input_shape[1]
     52         self.input_spec = InputSpec(dtype=K.floatx(), shape=(None, input_dim))
---> 53         self.clusters = self.add_weight((self.n_clusters, input_dim), initializer='glorot_uniform', name='clustering')
     54         if self.initial_weights is not None:
     55             self.set_weights(self.initial_weights)

TypeError: add_weight() got multiple values for argument 'name'

Any ideas on how to deal with this will be much appreciated!

jianhuupenn commented 4 years ago

Hi Cartal, I think this is a common issue for Keras package. Here I suggest 2 ways to solve it: 1) Make sure your environment is the same as one of the environment I listed, especially the version of Keras, since I have never encounter this error using my environments. 2) If you don't want to change your current environment, I think this page may be helpful: https://github.com/keras-team/keras/issues/13540#issuecomment-554213632 Please let me know if you still get the error.

Jian

cartal commented 4 years ago

Hi,

I'm happy to change my environment. I did so because my version of Keras was keras = 2.4 instead of keras = 2.2.4. Now I have keras = 2.2.4 but I get the following error:

/opt/conda/lib/python3.7/site-packages/scanpy/preprocessing/_simple.py:297: DeprecationWarning: Use is_view instead of isview, isview will be removed in the future.
  if isinstance(data, AnnData) and data.isview:
the var_names of adata.raw: adata.raw.var_names.is_unique=: True
/opt/conda/lib/python3.7/site-packages/scanpy/preprocessing/_simple.py:297: DeprecationWarning: Use is_view instead of isview, isview will be removed in the future.
  if isinstance(data, AnnData) and data.isview:
/opt/conda/lib/python3.7/site-packages/scanpy/_utils.py:341: DeprecationWarning: Use is_view instead of isview, isview will be removed in the future.
  if adata.isview:
/opt/conda/lib/python3.7/site-packages/scanpy/preprocessing/_simple.py:297: DeprecationWarning: Use is_view instead of isview, isview will be removed in the future.
  if isinstance(data, AnnData) and data.isview:
the var_names of adata.raw: adata.raw.var_names.is_unique=: True
/opt/conda/lib/python3.7/site-packages/scanpy/preprocessing/_simple.py:297: DeprecationWarning: Use is_view instead of isview, isview will be removed in the future.
  if isinstance(data, AnnData) and data.isview:
/opt/conda/lib/python3.7/site-packages/scanpy/_utils.py:341: DeprecationWarning: Use is_view instead of isview, isview will be removed in the future.
  if adata.isview:
The number of training celltypes is:  6
Training the source network
The layer numbers are[128, 32]
The shape of xtrain is:48449:1999
The shape of xtest is:12163:1999
Doing DEC: pretrain
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-15-5f83b1f46491> in <module>
----> 1 clf.fit(lymphoid_train, lymphoid_test, epochs_fit = 100)

/opt/conda/lib/python3.7/site-packages/ItClust/ItClust.py in fit(***failed resolving arguments***)
    139         print(":".join(["The shape of xtest is",str(x_test.shape[0]),str(x_test.shape[1])]))
    140         assert x_train.shape[1]==x_test.shape[1]
--> 141         dec=DEC(dims=dims,y=y_train,x=x_train,alpha=alpha,init=self.init,pretrain_epochs=self.pretrain_epochs,actinlayer1="tanh",softmax=softmax)
    142         dec.compile(optimizer=SGD(lr=0.01,momentum=0.9))
    143         #print("dec.init_centroid",type(dec.init_centroid),dec.init_centroid)

/opt/conda/lib/python3.7/site-packages/ItClust/DEC.py in __init__(self, dims, x, y, alpha, init, n_clusters, louvain_resolution, n_neighbors, pretrain_epochs, ae_weights, actinlayer1, is_stacked, transfer_feature, model_weights, y_trans, softmax)
    115         self.pretrain_epochs=pretrain_epochs
    116         if  self.transfer_feature is None:
--> 117             self.pretrain(n_neighbors=n_neighbors,epochs=self.pretrain_epochs,n_clusters=n_clusters)
    118         else:
    119             self.pretrain_transfer(n_neighbors=n_neighbors,model_weights=self.model_weights,features=transfer_feature,epochs=self.pretrain_epochs,n_clusters=n_clusters,y_trans=self.y_trans)

/opt/conda/lib/python3.7/site-packages/ItClust/DEC.py in pretrain(self, optimizer, epochs, n_neighbors, batch_size, n_clusters)
    121     def pretrain(self, optimizer='adam', epochs=200, n_neighbors=10,batch_size=256,n_clusters=None):
    122         print("Doing DEC: pretrain")
--> 123         sae=SAE(dims=self.dims,drop_rate=0.2,batch_size=batch_size,actinlayer1=self.actinlayer1)# batch_size
    124         print('...Pretraining...')
    125         # begin pretraining

/opt/conda/lib/python3.7/site-packages/ItClust/SAE.py in __init__(self, dims, act, drop_rate, batch_size, actinlayer1, init)
     38         self.init=init
     39         self.batch_size = batch_size
---> 40         self.stacks = [self.make_stack(i) for i in range(self.n_stacks)]
     41         self.autoencoders ,self.encoder= self.make_autoencoders()
     42         #plot_model(self.autoencoders, show_shapes=True, to_file='autoencoders.png')

/opt/conda/lib/python3.7/site-packages/ItClust/SAE.py in <listcomp>(.0)
     38         self.init=init
     39         self.batch_size = batch_size
---> 40         self.stacks = [self.make_stack(i) for i in range(self.n_stacks)]
     41         self.autoencoders ,self.encoder= self.make_autoencoders()
     42         #plot_model(self.autoencoders, show_shapes=True, to_file='autoencoders.png')

/opt/conda/lib/python3.7/site-packages/ItClust/SAE.py in make_stack(self, ith)
     82         if ith == self.n_stacks-1:
     83             hidden_act = self.actinlayer1 #tanh, or linear
---> 84         model = Sequential()
     85         model.add(Dropout(self.drop_rate, input_shape=(in_out_dim,)))
     86         model.add(Dense(units=hidden_dim, activation=hidden_act, name='encoder_%d' % ith))

/opt/conda/lib/python3.7/site-packages/keras/engine/sequential.py in __init__(self, layers, name)
     85 
     86     def __init__(self, layers=None, name=None):
---> 87         super(Sequential, self).__init__(name=name)
     88         self._build_input_shape = None
     89 

/opt/conda/lib/python3.7/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name + '` call to the ' +
     90                               'Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

/opt/conda/lib/python3.7/site-packages/keras/engine/network.py in __init__(self, *args, **kwargs)
     94         else:
     95             # Subclassed network
---> 96             self._init_subclassed_network(**kwargs)
     97 
     98     def _base_init(self, name=None):

/opt/conda/lib/python3.7/site-packages/keras/engine/network.py in _init_subclassed_network(self, name)
    292 
    293     def _init_subclassed_network(self, name=None):
--> 294         self._base_init(name=name)
    295         self._is_graph_network = False
    296         self._expects_training_arg = has_arg(self.call, 'training')

/opt/conda/lib/python3.7/site-packages/keras/engine/network.py in _base_init(self, name)
    107         if not name:
    108             prefix = self.__class__.__name__.lower()
--> 109             name = prefix + '_' + str(K.get_uid(prefix))
    110         self.name = name
    111 

/opt/conda/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in get_uid(prefix)
     72     """
     73     global _GRAPH_UID_DICTS
---> 74     graph = tf.get_default_graph()
     75     if graph not in _GRAPH_UID_DICTS:
     76         _GRAPH_UID_DICTS[graph] = defaultdict(int)

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'

It looks like there is a compatibility issue with Keras and tensorflow, as described here and here. I have followed these solutions but I always get the same error.

Could you share your environment to see if there's an issue with our tensorflow versions?

jianhuupenn commented 4 years ago

Sure thing. Currently, we are running ItClust using 2 environments and both works well. 1) System: Mac OS, keras = 2.2.4, tensorflow = 1.14.0. 2) System: Mac OS, keras = 2.1.5, tensorflow = 1.7.0.

cartal commented 4 years ago

Hi, thanks that solved my issue. I needed to install keras==2.2.4 together with tensorflow==1.14.0 and then it worked.