帮助我的训练数据

数据挖掘 Python 张量流
2022-02-26 21:03:28

我正在按照 tensorflow tut 开发我的第一个 NN,并尝试使用我自己的数据。经过大约 80 次尝试格式化我的数据并尝试将其加载到数据集中进行训练后,我放弃了。

这是我的数据目前的样子

syslog_data = [
[302014,0,0,63878,30,3,1], [302014,0,0,3891,0,0,0], [302014,0,0,15928,0,0,2], [305013,5,0,123,99999,0,3],
[302014,0,0,5185,0,0,0], [305013,5,0,123,99999,0,3], [302014,0,0,56085,0,0,0], [110002,4,2,50074,99999,0,4],

在此每个列表中的最后一项是标签。如果您能告诉我是否需要重新格式化我的数据以及如何或只是如何将其正确加载到数据集中。

感谢您提供的任何帮助或建议

这是完整的代码:

import tensorflow as tf
import numpy as np
from tensorflow.keras import layers
from . import syslog

print(tf.VERSION)
print(tf.keras.__version__)

model = tf.keras.Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(layers.Dense(64, activation='relu'))
# Add another:
model.add(layers.Dense(64, activation='relu'))
# Add a softmax layer with 10 output units:
model.add(layers.Dense(10, activation='softmax'))

model.compile(optimizer=tf.train.AdamOptimizer(0.001),
              loss='categorical_crossentropy',
              metrics=['accuracy'])

dataset = tf.data.dataset.from_tensor_slices(syslog)

model.fit(dataset, epochs=10, steps_per_epoch=30)
2个回答

您可能希望将一些问题和内容添加到现有脚本中。

下面我将您的示例数据分成两个 NumPy 数组:

  • 输入值x
  • 标签y

确保它们是 type 也很重要float32,因为如果你传递整数,Tensorflow 会报错(否则它们会被解释)。

以下对我有用,模型训练完成:

import numpy as np
import tensorflow as tf
from tensorflow.keras import layers

syslog_data = [
    [302014, 0, 0, 63878, 30, 3, 1],
    [302014, 0, 0, 3891, 0, 0, 0],
    [302014, 0, 0, 15928, 0, 0, 2],
    [305013, 5, 0, 123, 99999, 0, 3],
    [302014, 0, 0, 5185, 0, 0, 0],
    [305013, 5, 0, 123, 99999, 0, 3],
    [302014, 0, 0, 56085, 0, 0, 0],
    [110002, 4, 2, 50074, 99999, 0, 4],
]

print(tf.VERSION)
print(tf.keras.__version__)

x = np.array([arr[:-1] for arr in syslog_data], dtype=np.float32)
y = np.array([arr[-1:] for arr in syslog_data], dtype=np.float32)

model = tf.keras.Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(layers.Dense(64, activation="relu"))
# Add another:
model.add(layers.Dense(64, activation="relu"))
# Add a softmax layer with 10 output units:
model.add(layers.Dense(10, activation="softmax"))

model.compile(optimizer=tf.train.AdamOptimizer(0.001), loss="categorical_crossentropy", metrics=["accuracy"])

model.fit(x, y, epochs=10, steps_per_epoch=30)
import keras
import numpy as np
full_data = np.array(syslog_data)
X = full_data[:,:6]
Y = full_data[:,6]
# Convert labels to categorical one-hot encoding
one_hot_labels = keras.utils.to_categorical(Y, num_classes=10)

model.fit(X,Y, epochs=10, steps_per_epoch=30)

这行得通吗?我想我可能误解了这个问题。