2

I'm trying to write this code into colab. Interestingly, I was running the same code in colab a few days ago but now it won't work. the code also works in kaggle kernel. I tried changing the TensorFlow version but all of them give different errors. Why do you think I can't run this code? This is the colab notebook if you needed more info. Thanks in advance!

class DisasterDetector:

def __init__(self, tokenizer, bert_layer, max_len =30, lr = 0.0001,
             epochs = 15, batch_size = 32, dtype = tf.int32 ,
             activation = 'sigmoid', optimizer = 'SGD',
             beta_1=0.9, beta_2=0.999, epsilon=1e-07,
             metrics = 'accuracy', loss = 'binary_crossentropy'):
    
    self.lr = lr
    self.epochs = epochs
    self.max_len = max_len
    self.batch_size = batch_size
    self.tokenizer = tokenizer
    self.bert_layer = bert_layer
    self.models = []

    self.activation = activation
    self.optimizer = optimizer
    self.dtype = dtype
    
    self.beta_1 = beta_1
    self.beta_2 = beta_2
    self.epsilon =epsilon
    
    self.metrics = metrics
    self.loss = loss
    
def encode(self, texts):
    all_tokens = []
    masks = []
    segments = []
    
    for text in texts:
        
        tokenized = self.tokenizer.convert_tokens_to_ids(['[CLS]'] + self.tokenizer.tokenize(text) + ['[SEP]'])
        
        len_zeros = self.max_len - len(tokenized)
        
        
        padded = tokenized + [0] * len_zeros
        mask = [1] * len(tokenized) + [0] * len_zeros
        segment = [0] * self.max_len
        
        all_tokens.append(padded)
        masks.append(mask)
        segments.append(segment)
        
    print(len(all_tokens[0]))
    return np.array(all_tokens), np.array(masks), np.array(segments)
    
def make_model(self):
    

    input_word_ids = Input(shape = (self.max_len, ), dtype=tf.int32,
                        name = 'input_word_ids')
    
    input_mask = Input(shape = (self.max_len, ), dtype=tf.int32,
                       name = 'input_mask')
    
    segment_ids = Input(shape = (self.max_len, ), dtype=tf.int32,
                        name = 'segment_ids')


    #pooled output is the output of dimention and

    pooled_output, sequence_output = self.bert_layer([input_word_ids,
                                                 input_mask,
                                                 segment_ids])

    clf_output = sequence_output[:, 0, :]
    out = tf.keras.layers.Dense(1, activation = self.activation)(clf_output)
    #out = tf.keras.layers.Dense(1, activation = 'sigmoid', input_shape =  (clf_output,) )(clf_output)
    

    model = Model(inputs = [input_word_ids, input_mask, segment_ids],
                  outputs = out)
    if self.optimizer is 'SGD':
        optimizer = SGD(learning_rate = self.lr)

    elif self.optimizer is 'Adam': 
        optimizer = Adam(learning_rate = self.lr, beta_1=self.beta_1,
                         beta_2=self.beta_2, epsilon=self.epsilon)

    model.compile(loss = self.loss, optimizer = self.optimizer,
                  metrics = [self.metrics])
    
    return model




def train(self, x, k = 3):    
    kfold = StratifiedKFold(n_splits = k, shuffle = True)


    for fold, (train_idx, val_idx) in enumerate(kfold.split(x['cleaned_text'], x['target'])):
        print('fold: ', fold)

        x_trn = self.encode(x.loc[train_idx, 'cleaned_text'])
        x_val = self.encode(x.loc[val_idx, 'cleaned_text'])
        y_trn = np.array(x.loc[train_idx, 'target'], dtype = np.uint8)
        y_val = np.array(x.loc[val_idx, 'target'], dtype = np.uint8)
        print('the data type of y train: ', type(y_trn))
        print('x_val shape', x_val[0].shape)
        print('x_trn shape', x_trn[0].shape)
        
        model = self.make_model()
        print('model made.')
        model.fit(x_trn, tf.convert_to_tensor(y_trn),
                validation_data = (x_val, tf.convert_to_tensor(y_val)),
                batch_size=self.batch_size, epochs = self.epochs)

        self.models.append(model)

and after calling the train function of the class I get that error.

classifier = DisasterDetector(tokenizer = tokenizer, bert_layer = bert_layer, max_len = max_len, lr = 0.0001,
                  epochs = 10,  activation = 'sigmoid',
                batch_size = 32,optimizer = 'SGD',
                beta_1=0.9, beta_2=0.999, epsilon=1e-07)
classifier.train(train_cleaned)

and here is the error:

ValueError                                Traceback (most 

recent call last)
<ipython-input-10-106c756f2e47> in <module>()
----> 1 classifier.train(train_cleaned)

8 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
     96       dtype = dtypes.as_dtype(dtype).as_datatype_enum
     97   ctx.ensure_initialized()
---> 98   return ops.EagerTensor(value, ctx.device_name, dtype)
     99 
    100 

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list).
8
  • 1
    please provide the code and not just link to it. Going by the title you are trying to convert a list and not a numpy array. Commented Aug 19, 2020 at 8:18
  • The code is rather long so I thought it's better to provide the link. But here it is. Commented Aug 19, 2020 at 11:12
  • see here: stackoverflow.com/help/minimal-reproducible-example Commented Aug 19, 2020 at 11:14
  • Thanks for the link I'll read it. But for now I added the code that is giving me the error in the main question. Commented Aug 19, 2020 at 11:19
  • Can you set dtype=np.float32 instead of np.uint8 for y_trn and y_val? Commented Aug 19, 2020 at 11:52

1 Answer 1

1

Well, it turns out that by not giving the appropriate maximum sequence length, TensorFlow throws this error. By changing the max_len variable to 54 I could run my program with no difficulty. So the problem was not about the type of the input or the numpy arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

can you please help in this stackoverflow.com/questions/68141489/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.