我是tensorflow和建立神经网络的新手。
我正在尝试使用tf.keras api创建一个神经网络,该网络将接受一个输入,并提供3个输出。这是我的代码:
$insert_every_paragraphs = 2;
$minimum_paragraph_count = 1;
$adcodes = ['x100', 'x101', 'x102', 'x103'];
$paragraphs = [];
$split = explode('</p>',$content);
foreach($split as $paragraph){
//filter out empty paragraphs
if(strlen($paragraph)>3)
$paragraphs[] = $paragraph . '</p>';
}
$paragraph_count = count($paragraphs);
$ad_num = 0;
$counter = $minimum_paragraph_count;
for($i=0;$i<$paragraph_count;$i++){
if($counter==0){
$adcode = $adcodes[$ad_num];
$shortcode = "[cms_ad:$adcode]";
array_splice($paragraphs,$i+$ad_num,0,$shortcode);
$counter = $insert_every_paragraphs;
$ad_num++;
if($ad_num>=count($adcodes))
break;
}
$counter--;
}
$content = implode('',$paragraphs);
我已指定最后一层应具有三个输出,但是每次运行它时都会出现此错误:
import os
import tensorflow as tf
from tensorflow import keras
import numpy as np
train_times = np.array([[1],[2],[3],[4],[5],[6],[7],[8]])
train_sensors = np.array([[0.1,0.15,0.2],[0.25,0.3,0.35],[0.4,0.45,0.5],[0.55,0.6,0.65],[0.7,0.75,0.8],[0.85,0.9,0.95],[0.05,0.33,0.56],[0.8,0.35,0.9]])
test_times = np.array([[1],[2],[3],[4],[5],[6],[7],[8]])
test_sensors = np.array([[0.1,0.15,0.2],[0.25,0.3,0.35],[0.4,0.45,0.5],[0.55,0.6,0.65],[0.7,0.75,0.8],[0.85,0.9,0.95],[0.05,0.33,0.56],[0.8,0.35,0.9]])
print(train_sensors[0].shape)
def create_model():
model = tf.keras.models.Sequential([
keras.layers.Dense(5, activation=tf.nn.relu, input_shape=(1,), name="Input"),
keras.layers.Dense(10,activation=tf.nn.relu, name="Middle"),
keras.layers.Dropout(0.2),
keras.layers.Dense(3, activation=tf.nn.softmax, name="Out")
])
model.compile(optimizer=tf.keras.optimizers.Adam(), loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
return model
model = create_model()
model.summary()
checkpoint_path = "sensor_predict.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path,save_weights_only=True,verbose=1)
model.fit(x=train_times, y=train_sensors,epochs = 10,validation_data = (test_sensors, test_times), callbacks = [cp_callback])
我无法弄清楚为什么似乎认为我想要网络中的单个输出。
注意:我使用的数据集不是我将实际使用的数据集。我只是想建立一个功能正常的网络,然后稍后再生成数据。
答案 0 :(得分:1)
您的损失函数(tf.keras.losses.sparse_categorical_crossentropy)期望训练向量是一种热编码的。例如,将其更改为tf.keras.losses.mse,我认为它会起作用。
有关定义,请参见tensorflow docs。