BIMSBbioinfo / janggu

Deep learning infrastructure for genomics
GNU General Public License v3.0
254 stars 33 forks source link

keras.engine.topology #20

Closed starboyvarun closed 3 years ago

starboyvarun commented 3 years ago

1

Whenever I am trying to run any src folder file. It gives this error. I have already tried this from stackoverflow: from tensorflow.keras.layers import Layer, InputSpec , and several other things but still it's showing this error. Can please help me solve this. ThankYou.

wkopp commented 3 years ago

Could you tell me which keras and tensorflow version you are using?

starboyvarun commented 3 years ago

2

just because of this error I can't get forward. Please also add me to LinkedIn with the name Varun Allen. I really need to solve this bug by end of tomorrow. Thank You @wkopp

wkopp commented 3 years ago

The keras/tensorflow versions are not compatible with the janggu version you are using. Try to follow the installation instructions on the readme page (https://github.com/BIMSBbioinfo/janggu#readme). As explained there, for tensorflow 2 you can use tensorflow==2.2 and keras==2.4.3

starboyvarun commented 3 years ago

3 3 1

reading the readme page, I installed it showed the same version. And again gave the same error.

starboyvarun commented 3 years ago

4 4 1

while using other commands in the readme page still its showing 2.6.0 version

wkopp commented 3 years ago

Then the installation of the corresponding package versions seems to have failed, as is suggested by the conflict messages that you've posted above. Eventually you should have tensorflow==2.2 and keras==2.4.3 installed.

starboyvarun commented 3 years ago

But I tried installing it. using the above command can you tell me what should I do now? how I can install tensorflow==2.2 and keras==2.4.3 without conflict? Thankyou.

wkopp commented 3 years ago

Please note that these issues are not related to janggu, but to other packages (tensorflow and keras) and the particular environment and perhaps the google collab's specific settings that you use, which I am not familiar with. You could try uninstall and reinstall keras and tensorflow to see if you could overcome the package conflicts or try to use a fresh environment in which you don't get the conflicts.

anoopkdcs commented 3 years ago

insted of -- from keras.engine.topology import Layer -- try options 1 or 2

  1. from tensorflow.keras.layers import Layer
  2. from keras.layers import Layer
starboyvarun commented 3 years ago

@anoopkdcs Thank you for replying. I already solved this issue. If you are using janggu in google collab and facing any issue let me know. I can help you. Thank You.

wkopp commented 3 years ago

@anoopkdcs That is correct, thank you. I'll adapt the import.

maheshwaran-p commented 3 years ago

ModuleNotFoundError: No module named 'keras.engine.topology'

I Got the Same error .

maheshwaran-p commented 3 years ago

Help Me to solve this problem . Screenshot (236)

maheshwaran-p commented 3 years ago

import tflearn import numpy as np from sklearn.manifold import TSNE import matplotlib.pyplot as plt from tflearn.layers.core import input_data, dropout, fully_connected from tflearn.layers.conv import conv_1d, global_max_pool from tflearn.layers.merge_ops import merge from tflearn.layers.estimator import regression import tensorflow as tf import os os.environ['KERAS_BACKEND']='theano' from keras.layers import Embedding from keras.layers import Dense, Input, Flatten from keras.layers import Conv1D, MaxPooling1D, Embedding, concatenate, Dropout, LSTM, GRU, Bidirectional from keras.models import Model,Sequential

from keras import backend as K from keras.engine.topology import Layer, InputSpec from keras.layers import Layer from keras import initializers, optimizers

def lstm_keras(inp_dim, vocab_size, embed_size, num_classes, learn_rate):

K.clear_session()

model = Sequential()
model.add(Embedding(vocab_size, embed_size, input_length=inp_dim, trainable=True))
model.add(Dropout(0.25))
model.add(LSTM(embed_size))
model.add(Dropout(0.50))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',
          optimizer='adam',
          metrics=['accuracy'])
return model

def cnn(inp_dim, vocab_size, embed_size, num_classes, learn_rate): tf.reset_default_graph() network = input_data(shape=[None, inp_dim], name='input') network = tflearn.embedding(network, input_dim=vocab_size, output_dim=embed_size, name="EmbeddingLayer") network = dropout(network, 0.25) branch1 = conv_1d(network, embed_size, 3, padding='valid', activation='relu', regularizer="L2", name="layer_1") branch2 = conv_1d(network, embed_size, 4, padding='valid', activation='relu', regularizer="L2", name="layer_2") branch3 = conv_1d(network, embed_size, 5, padding='valid', activation='relu', regularizer="L2", name="layer_3") network = concatenate([branch1, branch2, branch3], mode='concat', axis=1) network = tf.expand_dims(network, 2) network = global_max_pool(network) network = dropout(network, 0.50) network = fully_connected(network, num_classes, activation='softmax', name="fc") network = regression(network, optimizer='adam', learning_rate=learn_rate, loss='categorical_crossentropy', name='target')

model = tflearn.DNN(network, tensorboard_verbose=0)
return model

def blstm(inp_dim,vocab_size, embed_size, num_classes, learn_rate):

K.clear_session()

model = Sequential()
model.add(Embedding(vocab_size, embed_size, input_length=inp_dim, trainable=True))
model.add(Dropout(0.25))
model.add(Bidirectional(LSTM(embed_size)))
model.add(Dropout(0.50))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',
          optimizer='adam',
          metrics=['accuracy'])

return model

class AttLayer(Layer):

def __init__(self, **kwargs):
    super(AttLayer, self).__init__(**kwargs)

def build(self, input_shape):
    # Create a trainable weight variable for this layer.
    self.W = self.add_weight(name='kernel', 
                                  shape=(input_shape[-1],),
                                  initializer='random_normal',
                                  trainable=True)
    super(AttLayer, self).build(input_shape)  # Be sure to call this somewhere!

def call(self, x, mask=None):
    eij = K.tanh(K.dot(x, self.W))

    ai = K.exp(eij)
    weights = ai/K.sum(ai, axis=1).dimshuffle(0,'x')

    weighted_input = x*weights.dimshuffle(0,1,'x')
    return weighted_input.sum(axis=1)

def compute_output_shape(self, input_shape):
    return (input_shape[0], input_shape[-1])

def blstm_atten(inp_dim, vocab_size, embed_size, num_classes, learn_rate):

K.clear_session()

model = Sequential()
model.add(Embedding(vocab_size, embed_size, input_length=inp_dim))
model.add(Dropout(0.25))
model.add(Bidirectional(LSTM(embed_size, return_sequences=True)))
model.add(AttLayer())
model.add(Dropout(0.50))
model.add(Dense(num_classes, activation='softmax'))
adam = optimizers.Adam(lr=learn_rate, beta_1=0.9, beta_2=0.999)
model.compile(loss='categorical_crossentropy',
          optimizer='adam',
          metrics=['accuracy'])
return model

def get_model(m_type,inp_dim, vocab_size, embed_size, num_classes, learn_rate): if m_type == 'cnn': model = cnn(inp_dim, vocab_size, embed_size, num_classes, learn_rate) elif m_type == 'lstm': model = lstm_keras(inp_dim, vocab_size, embed_size, num_classes, learn_rate) elif m_type == "blstm": model = blstm(inp_dim) elif m_type == "blstm_attention": model = blstm_atten(inp_dim, vocab_size, embed_size, num_classes, learn_rate) else: print ("ERROR: Please specify a correst model") return None return model

Cebsile998 commented 2 years ago

HY starboyvarus how did you solve the this ModuleNotFoundError: No module named 'keras.engine.topology'

WillNovus commented 2 years ago

@anoopkdcs Thank you for replying. I already solved this issue. If you are using janggu in google collab and facing any issue let me know. I can help you. Thank You.

Hi, how do you solve it with Jupyter Notebook and not Colab. I know it works on Colab but if you've got to use local machine, you know what I mean.

shubhamias12 commented 2 years ago

I am getting the same issue can anyone help

Ehsaanali commented 2 years ago

I am also getting same error cannot import name 'Network' from 'keras.engine.topology

wkopp commented 2 years ago

Which versions of tensorflow, keras and janggu are you using? The issue is likely related to using incompatible library versions. Please use: tensorflow==2.2, keras==2.4.3 and janggu==0.10.2 (or follow the alternative installation instructions on the readme page). Thank you very much