如何使用Python使用Tensorflow加载包含stackoverflow问题的数据集?

Tensorflow是Google提供的一种机器学习框架。它是一个开放源代码框架,与Python结合使用以实现算法,深度学习应用程序等等。它用于研究和生产目的。它具有优化技术,可帮助快速执行复杂的数学运算。

这是因为它使用了NumPy和多维数组。这些多维数组也称为“张量”。该框架支持使用深度神经网络。它具有高度的可扩展性,并带有许多流行的数据集。它使用GPU计算并自动进行资源管理。它带有大量的机器学习库,并且得到了良好的支持和记录。该框架具有运行深度神经网络模型,对其进行训练以及创建可预测各个数据集相关特征的应用程序的能力。

可以使用下面的代码行在Windows上安装'tensorflow'软件包-

pip install tensorflow

我们正在使用Google合作实验室来运行以下代码。Google Colab或Colaboratory可以帮助通过浏览器运行Python代码,并且需要零配置和对GPU(图形处理单元)的免费访问。协作已建立在Jupyter Notebook的基础上。以下是使用Python加载包含StackOverflow问题的数据集的代码片段-

示例

batch_size = 32
seed = 42
print("The training parameters have been defined")
raw_train_ds = preprocessing.text_dataset_from_directory(
   train_dir,
   batch_size=batch_size,
   validation_split=0.25,
   subset='training',
   seed=seed)
for text_batch, label_batch in raw_train_ds.take(1):
   for i in range(10):
      print("Question: ", text_batch.numpy()[i][:100], '...')
      print("Label:", label_batch.numpy()[i])

代码信用-https://www.tensorflow.org/tutorials/load_data/text

输出结果

The training parameters have been defined
Found 8000 files belonging to 4 classes.
Using 6000 files for training.
Question: b'"my tester is going to the wrong constructor i am new to programming so if i ask a
question that can' ...
Label: 1
Question: b'"blank code slow skin detection this code changes the color space to lab and using a
threshold finds' ...
Label: 3
Question: b'"option and validation in blank i want to add a new option on my system where i
want to add two text' ...
Label: 1
Question: b'"exception: dynamic sql generation for the updatecommand is not supported against
a selectcommand th' ...
Label: 0
Question: b'"parameter with question mark and super in blank, i\'ve come across a method that
is formatted like t' ...
Label: 1
Question: b'call two objects wsdl the first time i got a very strange wsdl. ..i would like to call the
object (i' ...
Label: 0
Question: b'how to correctly make the icon for systemtray in blank using icon sizes of any
dimension for systemt' ...
Label: 0
Question: b'"is there a way to check a variable that exists in a different script than the original
one? i\'m try' ...
Label: 3
Question: b'"blank control flow i made a number which asks for 2 numbers with blank and
responds with the corre' ...
Label: 0
Question: b'"credentials cannot be used for ntlm authentication i am getting
org.apache.commons.httpclient.auth.' ...
Label: 1

解释

  • 数据从磁盘上装入,并准备成适合于训练它的形式。

  • 'text_dataset_from_dataset'实用程序用于创建带标签的数据集。

  • “ tf.Data”是功能强大的工具集合,可用于构建输入管道。

  • 目录结构被传递到'text_dataset_from_dataset'实用程序。

  • StackOverflow问题数据集分为训练数据集和测试数据集。

  • 使用'validation_split'方法创建一个验证集。

  • 标签为0或1或2或3。

猜你喜欢