如何摆脱我的机器学习教程代码循环中冒号下的红色波浪线?



我是机器学习的初学者,我在for循环中不断收到此错误: 冒号下的"意外错误"和"解析器 16" (我使用VScode(

#binary text classification
import numpy as np
from keras.datasets import imdb
from keras import models
from keras import layers,optimizers
import pandas.plotting._matplotlib as plt
#loading data from the dataset
(train_data,train_labels), (test_data,test_labels) = imdb.load_data(num_words = 10000)
#keeping only top 10000 repeated words in th etraining data and discarding the others
#creating binary matrix of the given data
def vector_sequences(sequences,dimension = 1000):
results = np.zeros((len(sequences),dimension)
for i,sequence in enumerate(sequences):
results[i,sequence] = 1
return results
#feeding training data in the x branch
x_train = vector_sequences(train_data)
x_test = vector_sequences(test_data)
#vectorizing and feeding label in the y branch
y_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('floate32') 
#Adding model layers and definition
model = models.Sequential()
model.add(layers.Dense(16,activation='relu',input_shape = (10000,)))
model.add(layers.Dense(16,activation='relu'))
model.add(layers.Dense(1,activation='sigmoid'))
#making an optimizer and compiling
model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])

这里的问题与不正确的缩进有关:循环的开始额外缩进一次,返回也额外缩进一次。 固定代码如下:

import numpy as np
from keras.datasets import imdb
from keras import models
from keras import layers,optimizers
import pandas.plotting._matplotlib as plt
#loading data from the dataset
(train_data,train_labels), (test_data,test_labels) = imdb.load_data(num_words = 10000)
#keeping only top 10000 repeated words in th etraining data and discarding the others
#creating binary matrix of the given data
def vector_sequences(sequences,dimension = 1000):
results = np.zeros((len(sequences),dimension))
for i,sequence in enumerate(sequences):
results[i,sequence] = 1
return results
#feeding training data in the x branch
x_train = vector_sequences(train_data)
x_test = vector_sequences(test_data)
#vectorizing and feeding label in the y branch
y_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('floate32') 
#Adding model layers and definition
model = models.Sequential()
model.add(layers.Dense(16,activation='relu',input_shape = (10000,)))
model.add(layers.Dense(16,activation='relu'))
model.add(layers.Dense(1,activation='sigmoid'))
#making an optimizer and compiling
model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])

最新更新