为什么我的model.predict
输出2个值而不是1?
model = Sequential()
#model.add(Flatten(input_shape=[139, 28]))
model.add(Dense(32, input_dim=x_train.shape[1], activation='relu')) #input=number of inputs from data; 32=number of neurons in the layer
model.add(Dropout(0.25))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(8, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(1, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
model.fit(x_train, y_train, epochs=10, batch_size=64)
print("Generate predictions")
predictions = model.predict(x_train[:1])
print(x_train.shape)
print (predictions)
print("predictions shape:", predictions.shape)`
我得到了以下输出:
Epoch 1/10
InvalidArgumentError Traceback(most recent call last)in<cell line: 16>()14 model.compile(loss='sparse_categorical_crossentropy',optimizer = 'adam',metrics = ['accuracy'])15 # fit model ---> 16 model.fit(x_train,y_train,epochs=10,batch_size=64)17 18 #print(len(model.layers))
1 frames /usr/local/lib/python3.10/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name,num_outputs,inputs,attrs,ctx,name)50 try:51 ctx.ensure_initialized()---> 52 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle,device_name,op_name,53 inputs,attrs,num_outputs)54 except core._NotOkStatusException as e:
InvalidArgumentError:图形执行错误:
在节点“sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits”处检测到(最近一次调用):文件"/usr/lib/python3.10/runpy. py ",line 196,in_run_module_as_main return_run_code(code,main_globals,None,File "/usr/lib/python3.10/runpy. py",line 86,in_run_code exec(code,run_globals)File "/usr/local/lib/python3.10/dist-packages/ipykernel_launcher. py”,第16行,在app中。launch_new_instance()文件"/usr/local/lib/python3.10/dist-packages/traitlets/config/application. py”,第992行,在launch_instance app中。start()File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp. py”,第619行,在开始www.example.com_loop中。start()File "/usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio. py”,第195行,在start self中。异步_www.example.com_永远self.ioself._run_callback(functools. partial(callback,future))文件"/usr/local/lib/python3.10/dist-packages/tornado/ioloop. py ",line 738,in_run_callback ret = callback()File "/usr/local/lib/python3.10/dist-packages/tornado/gen. py",line 825,in inner self. ctx_run(self.run)文件"/usr/local/lib/python3.10/dist-packages/tornado/gen. py”,第786行,在运行中,yielded = self。gen. send(value)File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase. py”,第361行,在处理中_一个产率生成maybe_future(dispatch(* args))文件"/usr/local/lib/python3.10/dist-packages/tornado/gen. py ",line 234,in wrapper yielded = ctx_run(next,result)File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase. py”,第261行,在dispatch_shell yield gen中。maybe_future(handler(stream,idents,msg))文件"/usr/local/lib/python3.10/dist-packages/tornado/gen. py",line 234,in wrapper yielded = ctx_run(next,result)File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase. py",第539行,在execute_request中loop.run www.example.com_ast_nodes(code_ast. body,cell_name,File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell. py ",line 3473,in run_ast_nodes if(await www.example.com_code(code,result,async = asy)):文件"/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell. py”,第3553行,在run_code exec(code_obj,self. user_global_ns,self. user_ns)文件"",第16行,在www.example.com(x_train,y_train,epochs = 10,batch_size = 64)文件"/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils. py ",line 65,in error_handler return fn( args,kwargs)File "/usr/local/lib/python3.10/dist-packages/keras/engine/training. py”,第1685行,在fit tmp_logs = self中。train_function(iterator)文件"/usr/local/lib/python3.10/dist-packages/keras/engine/training. py",line 1284,in train_function return step_function(self,iterator)File "/usr/local/lib/python3.10/dist-packages/keras/engine/training. py”,第1268行,在step_function outputs = model中。分布www.example.com(run_step,args =(data,))File "/usr/local/lib/python3.10/dist-packages/keras/engine/training. py”,第1249行,在run_step outputs = model中。train_step(data)文件"/usr/local/lib/python3.10/dist-packages/keras/engine/training. py”,第1051行,在train_step loss = self中。compute_loss(x,y,y_pred,sample_weight)文件"/usr/local/lib/python3.10/dist-packages/keras/engine/training. py”,第1109行,在compute_loss中返回self。compiled_loss(File "/usr/local/lib/python3.10/dist-packages/keras/engine/compile_utils. py ",line 265,incallloss_value = loss_obj(y_t,y_p,sample_weight = sw)File "/usr/local/lib/python3.10/dist-packages/keras/losses. py",line 142,incalllosses = call_fn(y_true,y_pred)File "/usr/local/lib/python3.10/dist-packages/keras/losses. py",第268行,在调用return ag_fn(y_true,y_pred,self._fn_kwargs)文件"/usr/local/lib/python3.10/dist-packages/keras/losses. py",line 2078,in sparse_categorical_crossentropy return backend. sparse_categorical_crossentropy(File "/usr/local/lib/python3.10/dist-packages/keras/backend. py”,第5660行,在sparse_categorical_crossentropy res = tf中。nn. sparse_softmax_cross_entropy_with_logits(节点: www.example.com 1 1 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 shell.run_cell(code, store_history=store_history, silent=silent) File "/usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py", line 539, in run_cell return super(ZMQInteractiveShell, self).run_cell(*args,kwargs) File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 2975, in run_cell result = self._run_cell( File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3030, in run_cell return runner(coro) File "/usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py", line 78, in pseudo_sync_runner coro.send(None) File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3257, in run_cell_async has_raised = await www.example.com _ast_nodes(code_ast.body, cell_name, File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3473, in run_ast_nodes if (await self.run_code(code, result, async=asy)): File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 16, in <cell line: 16>model.fit (x_train, y_train, epochs=10, batch_size=64) File "/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py", line 65, in error_handler return fn(*args,**kwargs) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1685, in fit tmp_logs = self.train_function(iterator) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1284, in train_function return step_function(self, iterator) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1268, in step_function outputs = model.distribute www.example.com (run_step, args=(data,)) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1249, in run_step outputs = model.train_step(data) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1051, in train_step loss = self.compute_loss(x, y, y_pred, sample_weight) File "/usr/local/lib/python3.10/dist-packages/keras/engine/training.py", line 1109, in compute_loss return self.compiled_loss( File "/usr/local/lib/python3.10/dist-packages/keras/engine/compile_utils.py", line 265, incallloss_value = loss_obj(y_t, y_p, sample_weight=sw) File "/usr/local/lib/python3.10/dist-packages/keras/losses.py", line 142, incall**losses = call_fn(y_true, y_pred) File "/usr/local/lib/python3.10/dist-packages/keras/losses.py", line 268, in call return ag_fn(y_true, y_pred,**self._fn_kwargs) File "/usr/local/lib/python3.10/dist-packages/keras/losses.py", line 2078, in sparse_categorical_crossentropy return backend.sparse_categorical_crossentropy( File "/usr/local/lib/python3.10/dist-packages/keras/backend.py", line 5660, in sparse_categorical_crossentropy res = tf.nn.sparse_softmax_cross_entropy_with_logits( Node: 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits' Received a label value of 1 which is outside the valid range of [0, 1). Label values: 1 1 0 1 0 1 1 0 1 1 1 1 1 0 1 0 0 1 0 1 1 1 1 0 1 1 0 1 1 1 0 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 0 {{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}} [Op:__inference_train_function_3244]
1条答案
按热度按时间vuv7lop31#
最后一个SoftMax层中有2个单位。因此,将有2个输出。
0.36325565
和0.6367443
是两个不同的输出。