Home > other >  NLP tag more input tensor size error problem
NLP tag more input tensor size error problem

Time:04-19

Introduction to vegetables chicken run code is not up, bosses help children
 Traceback (the most recent call last) : 
The File "D: \ WJYworkplace \ PythonWorkPlace \ NLP \ multi_label_classfication \ keras_albert_multi_label_cls - master \ model_train py", line 164, in & lt; module>
Validation_steps=len (test_D)
The File "D: \ MyGeekTool \ DevelopTool \ MicrosoftVisualStudio \ Shared \ Python37_64 \ lib \ site - packages \ tensorflow \ python \ keras \ engine \ training py", line 1861, in fit_generator
Initial_epoch=initial_epoch)
The File "D: \ MyGeekTool \ DevelopTool \ MicrosoftVisualStudio \ Shared \ Python37_64 \ lib \ site - packages \ tensorflow \ python \ keras \ engine \ training py", line 1100, fit in
Tmp_logs=self. Train_function (iterator)
The File "D: \ MyGeekTool \ DevelopTool \ MicrosoftVisualStudio \ Shared \ Python37_64 \ lib \ site - packages \ tensorflow \ python \ eager \ def_function py", line 828, in __call__
Result=self. _call (* args, * * KWDS)
The File "D: \ MyGeekTool \ DevelopTool \ MicrosoftVisualStudio \ Shared \ Python37_64 \ lib \ site - packages \ tensorflow \ python \ eager \ def_function py", line 888, in _call
Return the self. _stateless_fn (* args, * * KWDS)
File "D: \ MyGeekTool \ DevelopTool \ MicrosoftVisualStudio \ Shared \ Python37_64 \ lib \ site - packages \ tensorflow \ python \ eager \ function py", line 2943, in __call__
Filtered_flat_args, captured_inputs=graph_function. Captured_inputs) # pylint: disable=protected - access
File "D: \ MyGeekTool \ DevelopTool \ MicrosoftVisualStudio \ Shared \ Python37_64 \ lib \ site - packages \ tensorflow \ python \ eager \ function py", line 1919, in _call_flat
CTX, args, cancellation_manager=cancellation_manager))
File "D: \ MyGeekTool \ DevelopTool \ MicrosoftVisualStudio \ Shared \ Python37_64 \ lib \ site - packages \ tensorflow \ python \ eager \ function py", line 560, in the call
CTX=CTX)
File "D: \ MyGeekTool \ DevelopTool \ MicrosoftVisualStudio \ Shared \ Python37_64 \ lib \ site - packages \ tensorflow \ python, eager, execute py", 60, the line in quick_execute
Inputs, attrs, num_outputs)
Tensorflow. Python. Framework. Errors_impl. InvalidArgumentError: Input to reshape is a tensor with 374400 values, but the requested shape from the 1277952
[[node model_2/model_1/Embed - Token/Reshape_2 (defined at D: \ MyGeekTool \ DevelopTool \ MicrosoftVisualStudio \ Shared \ Python37_64 \ lib \ site - packages \ keras_adaptive_softmax \ embedding py: 172)]] [Op: __inference_train_function_12406]

The Function call stack:
Train_function

The training data input tensor wrong where to modify? , here is the code for the model training
 # - * - coding: utf-8 - * - 
The # @ Time: 2020/12/23 thrust
# @ Author: Jclian91
# @ File: model_train. Py
# @ Place: Shanghai Yangpu,
The import json
The import codecs
The import pandas as pd
The import numpy as np
The from keras_bert import Tokenizer
The from keras. The layers import *
The from keras. Models import Model
The from keras. Optimizers import Adam

The from Albert import load_brightmart_albert_zh_checkpoint

# suggested length & lt;=510
Maxlen=256
BATCH_SIZE=8
Dict_path='./albert_tiny/vocab. TXT '


Token_dict={}
With codecs. Open (dict_path, 'r', 'utf-8) as reader:
For the line in reader:
Token=line. Strip ()
Token_dict [token]=len (token_dict)


The class OurTokenizer (Tokenizer) :
Def _tokenize (self, text) :
R=[]
For c in the text:
If c in self. _token_dict:
Of state Richard armitage ppend (c)
The else:
State Richard armitage ppend (' [UNK] ') # the rest of the characters is [UNK]
Return R


Tokenizer=OurTokenizer (token_dict)


Def seq_padding (X, padding=0) :
L=[len (x) for x in x]
ML=Max (L)
Return np. Array ([
Np. Concatenate ([x, [padding] * (ML - len (x)]) if len (x) & lt; ML else for x in x x
])


The class DataGenerator:

Def __init__ (self, data, batch_size=batch_size) :
The self. The data=https://bbs.csdn.net/topics/data
The self. The batch_size=batch_size
Self. Steps=len (self. Data)//self. The batch_size
If len (self) data) % self. The batch_size!=0:
The self. The steps +=1

Def __len__ (self) :
Return the self. The steps

Def __iter__ (self) :
While True:
Idxs=list (range (len (self. Data)))
Np. Random. Shuffle (idxs)
X1, X2, Y=[], [], []
For I in idxs:
D=self. Data [I]
Text=d [0] [: maxlen]
X1, x2=tokenizer. Encode (first=text)
Y=d [1]
X1. Append (X1)
X2. Append (X2)
Y.a ppend (y)
If len (X1)==self. The batch_size or I==idxs [1] :
X1=seq_padding (X1)
X2=seq_padding (X2)
Y=seq_padding (Y)
Yield (X1, X2), Y
[X1, X2, Y]=[], [], []


# building model
Def create_cls_model (num_labels) :
Albert_tiny albert_model=load_brightmart_albert_zh_checkpoint (", "training=False)

For layer in albert_model. The layers:
Layer. The trainable=True

X1_in=Input (shape=(None,))
X2_in=Input (shape=(None,))

X=albert_model ([x1_in x2_in])
Cls_layer=Lambda (Lambda x: [0] :, x) (x) # remove the corresponding vector (CLS) used to make classification
P=Dense (num_labels, activation='sigmoid') (cls_layer) # more classification

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related