model = tf.keras.Sequential([
# tf.keras.layers.Dense is basically implementing: output = activation(dot(input, weight) + bias)
# it takes several arguments, but the most important ones for us are the hidden_layer_size and the activation function
tf.keras.layers.Dense(hidden_layer_size, activation='relu'), # 1st hidden layer
tf.keras.layers.Dense(hidden_layer_size, activation='relu'), # 2nd hidden layer
# the final layer is no different, we just make sure to activate it with softmax
tf.keras.layers.Dense(output_size, activation='sigmoid') # output layer
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
### Training
# That's where we train the model we have built.
# set the batch size
batch_size = 100
# set a maximum number of training epochs
max_epochs = 100
def nn_batch_generator(X_data, y_data, batch_size):
samples_per_epoch = X_data.shape[0]
number_of_batches = samples_per_epoch/batch_size
counter=0
index = np.arange(np.shape(y_data)[0])
while 1:
index_batch = index[batch_size*counter:batch_size*(counter+1)]
X_batch = X_data[index_batch,:].todense()
X_batch.dtype='float32'
print(X_batch)
y_batch = y_data[index_batch]
y_batch.astype(np.float64)
counter += 1
yield np.array(X_batch),y_batch
if (counter > number_of_batches):
counter=0
model.fit_generator(generator=nn_batch_generator(train_inputs, train_targets, batch_size),epochs=max_epochs,steps_per_epoch=300)
# fit the model
# note that this time the train, validation and test data are not iterable
# model.fit(train_inputs, # train inputs
# train_targets, # train targets
# batch_size=batch_size, # batch size
# epochs=max_epochs, # epochs that we will train for (assuming early stopping doesn't kick in)
# validation_data=(validation_inputs, validation_targets), # validation data
# verbose = 2 # making sure we get enough information about the training process
# )
test_loss, test_accuracy = model.evaluate(test_inputs, test_targets)
print('\nTest loss: {0:.2f}. Test accuracy: {1:.2f}%'.format(test_loss, test_accuracy*100.))
#inputs = tf.placeholder(tf.float32, [None, input_size])
错误是
InvalidArgumentError:接收到标签值1,该值超出有效范围[0,1)。标签值:1 1 0 0 1 0 0 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 0 1 1 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 1 0 0 1 0 1 0 1 0 0 0 0 1 1 1 1 1 1 1 0 1 0 1 0 0 0 0 0 0 0 1 1 0 1 0 1 1 0 1 1 0 1 1 0 0 1 0 1 0 1 0 1 [[{{node loss / output_1_loss / SparseSoftmaxCrossEntropyWithLogits / SparseSoftmaxCrossEntropyWithLogits}}]]
我认为S型函数的范围是[0,1],但是根据错误,它应该是(0,1)。 那我该如何解决呢?