#python #numpy #tensorflow #keras
#python #numpy #tensorflow #keras
Вопрос:
Я закодировал следующий. Каким-то образом это отображается на моем ноутбуке jupyter, но отлично работает в Google colab. Ниже приведен фрагмент моего кода, который выдает синтаксическую ошибку на моем рабочем столе, но не в Google colab.
Я был бы признателен, если кто-нибудь сможет ответить на мой вопрос. (пожалуйста, обратите внимание, что это НЕ весь код, я выбрал ФРАГМЕНТ кода, который выдает ошибку.)
Заранее благодарю вас.
#### PACKAGE IMPORTS ####
# Run this cell first to import all required packages.
# Do not make any imports elsewhere in the notebook
from numpy.random import seed
seed(8)
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets, model_selection
%matplotlib inline
# If you would like to make further imports from tensorflow, add them here
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, BatchNormalization
from tensorflow.keras import regularizers
# Complete the following function.
# Make sure to not change the function name or arguments.
def read_in_and_split_data(iris_data):
"""
This function takes the Iris dataset as loaded by sklearn.datasets.load_iris(), and then
splits so that the training set includes 90% of the full dataset, with the test set
making up the remaining 10%.
Your function should return a tuple (train_data, test_data, train_targets, test_targets)
of appropriately split training and test data and targets.
If you would like to import any further packages to aid you in this task, please do so in the
Package Imports cell above.
"""
from sklearn.model_selection import train_test_split
data = iris_data['data']
targets = iris_data['target']
train_data, test_data, train_targets, test_targets = train_test_split(data, targets, test_size=0.1)
return (train_data, test_data, train_targets, test_targets)
# Run your function to generate the test and training data.
from sklearn.datasets import load_iris
iris_data = datasets.load_iris()
train_data, test_data, train_targets, test_targets = read_in_and_split_data(iris_data)
# Convert targets to a one-hot encoding
train_targets = tf.keras.utils.to_categorical(np.array(train_targets))
test_targets = tf.keras.utils.to_categorical(np.array(test_targets))
# Define regularised_model
def get_regularised_model(input_shape, dropout_rate, weight_decay):
"""
This function should build a regularised Sequential model according to the above specification.
The dropout_rate argument in the function should be used to set the Dropout rate for all Dropout layers.
L2 kernel regularisation (weight decay) should be added using the weight_decay argument to
set the weight decay coefficient in all Dense layers that use L2 regularisation.
Ensure the weights are initialised by providing the input_shape argument in the first layer, given by the
function argument input_shape.
Your function should return the model.
"""
model = Sequential([
Dense(64, kernel_regularizer=regularizers.l2(weight_decay), activation="relu", input_shape=input_shape, use_bias=True, bias_initializer = 'ones', kernel_initializer='he_uniform'),
Dense(128, kernel_regularizer=regularizers.l2(weight_decay), activation="relu"),
Dense(128, kernel_regularizer=regularizers.l2(weight_decay), activation="relu"),
Dropout(dropout_rate)
Dense(128, kernel_regularizer=regularizers.l2(weight_decay), activation="relu" ),
Dense(128, kernel_regularizer=regularizers.l2(weight_decay), activation="relu" ),
BatchNormalization(),
Dense(64, kernel_regularizer=regularizers.L2(weight_decay), activation="relu"),
Dense(64, kernel_regularizer=regularizers.L2(weight_decay), activation="relu"),
Dropout(dropout_rate)
Dense(64, kernel_regularizer=regularizers.l2(weight_decay), activation='relu'),
Dense(64, kernel_regularizer=regularizers.l2(weight_decay), activation='relu'),
Dense(3, activation='softmax')
])
return model
Комментарии:
1. Пожалуйста, укажите полное сообщение об ошибке, иначе вы оставляете людей гадать, какая строка выдает ошибку.
2.сообщение об ошибке, пожалуйста, посчитайте строку из
def get_regularised_model
File "<ipython-input-10-23c90b4937c4>", line 17 Dense(128, kernel_regularizer=regularizers.l2(weight_decay), activation="relu" ),
сообщения об ошибке указывает, что она находится сразу послеDropout
^ SyntaxError: недопустимый синтаксис