亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何正確組織 Tensorflow 模型層?

如何正確組織 Tensorflow 模型層?

慕容森 2023-03-30 16:13:39
世界您好!我們正在編寫自己的 AI,并且努力創建正確的模型層。我們必須在我們的神經網絡中輸入的是一個list包含 nlists和 mtuplese.x. list = numpy.array([ [[1,2,4],[5,6,8]] , [[5,6,0],[7,2,4]] ])我們期望得到的結果是 0 或 1(相信我,這是有道理的)這就是我們現在所擁有的:tpl = 3 # because we have tuplesnl = 2 # number of lists we havemodel = tf.keras.Sequential([# this should be entry layer that understands our list            tf.keras.layers.Dense(nl * tpl , input_shape=(nl, tpl), activation='relu'),#hidden layers..            tf.keras.layers.Dense(64, input_shape=(nl, tpl), activation='sigmoid'),#our output layer with 2 nodes that one should contain 0, other 1, because we have 2 labels ( 0 and 1 )            tf.keras.layers.Dense(2, input_shape=(0, 1), activation='softmax')        ])但是我們得到以下錯誤:/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)     58     ctx.ensure_initialized()     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,---> 60                                         inputs, attrs, num_outputs)     61   except core._NotOkStatusException as e:     62     if name is not None:InvalidArgumentError:  Incompatible shapes: [56,2,2] vs. [56,1]     [[node huber_loss/Sub (defined at <ipython-input-25-08eb2e0b395e>:53) ]] [Op:__inference_train_function_45699]Function call stack:train_function如果我們總結我們的模型,它會給出以下結構:Layer (type)                 Output Shape              Param #   =================================================================dense_1 (Dense)             (None, 2, 6)              24        _________________________________________________________________dense_2 (Dense)             (None, 2, 64)             448       _________________________________________________________________dense_3 (Dense)             (None, 2, 2)              130       =================================================================最后,我們了解到,我們提供的數據與最后一層不兼容,那么我們如何將最后一層轉換為 => shape (None, 2)或者解決此錯誤的正確方法是什么?
查看完整描述

1 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

您可以在輸出層之前使用Flatten()或。GlobalAveragePooling1D完整示例:


import numpy

import tensorflow as tf


list = numpy.array([[[1., 2., 4.], [5., 6., 8.]], [[5., 6., 0.], [7., 2., 4.]]])


tpl = 3  

nl = 2   

model = tf.keras.Sequential([

    tf.keras.layers.Dense(nl * tpl, input_shape=(nl, tpl), activation='relu'),

    tf.keras.layers.Dense(64, input_shape=(nl, tpl), activation='sigmoid'),

    tf.keras.layers.GlobalAveragePooling1D(),

    tf.keras.layers.Dense(2, input_shape=(0, 1), activation='softmax')

])


model.build(input_shape=(nl, tpl))


model(list)

<tf.Tensor: shape=(2, 2), dtype=float32, numpy=

array([[0.41599566, 0.58400434],

       [0.41397247, 0.58602756]], dtype=float32)>

你不會只得到 0 和 1,你會得到每個班級的概率。你也應該隱藏內置關鍵字list。


Model: "sequential_4"

_________________________________________________________________

Layer (type)                 Output Shape              Param #   

=================================================================

dense_12 (Dense)             (None, 2, 6)              24        

_________________________________________________________________

dense_13 (Dense)             (None, 2, 64)             448       

_________________________________________________________________

global_average_pooling1d (Gl (None, 64)                0         

_________________________________________________________________

dense_14 (Dense)             (None, 2)                 130       

=================================================================

Total params: 602

Trainable params: 602

Non-trainable params: 0

_________________________________________________________________


查看完整回答
反對 回復 2023-03-30
  • 1 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號