tensorflow function笔记: dropout

Function

tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None)

With probability keep_prob, outputs the input element scaled up by 1 / keep_prob, otherwise outputs 0. The scaling is so that the expected sum is unchanged.
输入元素有keep_prob 的几率按比例扩大1 / keep_prob 倍,否则输出为0. 比例为 1 / keep_prob 是为了使期望的总和不变。

By default, each element is kept or dropped independently. If noise_shape is specified, it must be broadcastable to the shape of x, and only dimensions with noise_shape[i] == shape(x)[i] will make independent decisions. For example, if shape(x) = [k, l, m, n] and noise_shape = [k, 1, 1, n], each batch and channel component will be kept independently and each row and column will be kept or not kept together.

默认每个元素被保留或者扔掉是独立的。如果指定 noise_shape, 只有 noise_shape[i] == shape(x)[i] 的维度是独立的。 例如 shape(x) = [k, l, m, n] and noise_shape = [k, 1, 1, n], batch 和 channel 的元素被保留的概率是独立, row 和 column 状态相同。
x_out = tf.nn.dropout(x, 0.5, noise_shape = [k, 1, 1, n]),则对于固定的k和n和任意的l和m, x_out[k, l, m, n]相等。(一组image的tensor的四个维度分别称作 [batch, row, column, channel ] 或者 [batch, height, width, channel] )

Args

x: A tensor.

keep_prob: A scalar Tensor with the same type as x. The probability that each element is kept.

noise_shape: A 1-D Tensor of type int32, representing the shape for randomly generated keep/drop flags.

seed: A Python integer. Used to create random seeds. See tf.set_random_seed for behavior.

name: A name for this operation (optional).

Returns

A Tensor of the same shape of x.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我们可以使用TensorFlow来实现文本分类。首先需要下载数据集并安装TensorFlow。 1. 下载数据集 我们可以使用以下命令从GitHub上下载`waimai_10k.csv`数据集: ``` !wget https://raw.githubusercontent.com/SophonPlus/ChineseNlpCorpus/master/datasets/waimai_10k/waimai_10k.csv ``` 2. 安装TensorFlow 我们可以使用以下命令安装TensorFlow: ``` !pip install tensorflow ``` 3. 加载数据集 我们可以使用Pandas库来加载数据集: ```python import pandas as pd df = pd.read_csv('waimai_10k.csv') ``` 4. 数据预处理 在进行文本分类之前,我们需要对数据进行预处理。首先,我们将标签转换为数字,然后将数据集拆分为训练集和测试集。 ```python from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import train_test_split # 将标签转换为数字 le = LabelEncoder() df['label'] = le.fit_transform(df['label']) # 拆分数据集为训练集和测试集 train_df, test_df = train_test_split(df, test_size=0.2, stratify=df['label'], random_state=42) ``` 5. 特征工程 我们需要将文本数据转换为计算机可以理解的形式。在这里,我们可以使用词袋模型,并使用TF-IDF进行特征缩放。 ```python from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer # 使用词袋模型 count_vect = CountVectorizer() X_train_counts = count_vect.fit_transform(train_df['review']) X_test_counts = count_vect.transform(test_df['review']) # 使用TF-IDF进行特征缩放 tfidf_transformer = TfidfTransformer() X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts) X_test_tfidf = tfidf_transformer.transform(X_test_counts) ``` 6. 训练模型 我们可以使用TensorFlow的Keras API来训练模型。在这里,我们将使用一个简单的神经网络模型。 ```python from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Dropout # 定义神经网络模型 model = Sequential() model.add(Dense(64, input_dim=X_train_tfidf.shape[1], activation='relu')) model.add(Dropout(0.5)) model.add(Dense(32, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1, activation='sigmoid')) # 编译模型 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # 训练模型 model.fit(X_train_tfidf, train_df['label'], epochs=10, batch_size=32, validation_split=0.2) ``` 7. 评估模型 最后,我们可以使用测试集来评估模型的性能。 ```python # 在测试集上评估模型 score = model.evaluate(X_test_tfidf, test_df['label'], batch_size=32) print('Test loss:', score[0]) print('Test accuracy:', score[1]) ``` 完成以上步骤后,我们就可以使用TensorFlow对文本进行分类了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值