如何使用 Tensorflow 加载包含使用 Python 的 stackoverflow 问题的数据集?
Tensorflow 是 Google 提供的机器学习框架。它是一个开源框架,与 Python 结合使用,用于实现算法、深度学习应用程序等。它用于研究和生产目的。它具有优化技术,有助于快速执行复杂的数学运算。
这是因为它使用 NumPy 和多维数组。这些多维数组也称为"张量"。该框架支持使用深度神经网络。它具有高度可扩展性,并附带许多流行的数据集。它使用 GPU 计算并自动管理资源。它附带大量机器学习库,并且得到良好的支持和记录。该框架能够运行深度神经网络模型、对其进行训练,并创建预测相应数据集相关特征的应用程序。
可以使用以下代码在 Windows 上安装 ‘tensorflow’ 包 −
pip install tensorflow
我们使用 Google Colaboratory 运行以下代码。Google Colab 或 Colaboratory 有助于在浏览器上运行 Python 代码,无需配置,并可免费访问 GPU(图形处理单元)。Collaboratory 建立在 Jupyter Notebook 之上。以下是使用 Python − 加载包含 StackOverflow 问题的数据集的代码片段
示例
batch_size = 32 seed = 42 print("训练参数已定义") 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
输出
训练参数已定义 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。