tensorflow问题
20210121
ImportError: No module named ‘tensorflow.python’
https://stackoverflow.com/questions/41415629/importerror-no-module-named-tensorflow-python
卸載后安裝需要的tf版本
多標簽
def create_model(bert_config, is_training, input_ids, input_mask, segment_ids,labels, num_labels, use_one_hot_embeddings):"""Creates a classification model."""model = modeling.BertModel(config=bert_config,is_training=is_training,input_ids=input_ids,input_mask=input_mask,token_type_ids=segment_ids,use_one_hot_embeddings=use_one_hot_embeddings)# In the demo, we are doing a simple classification task on the entire# segment.## If you want to use the token-level output, use model.get_sequence_output()# instead.output_layer = model.get_pooled_output()print('output_layer的輸出:',output_layer[:5])#with tf.gfile.GFile(os.path.join(FLAGS.output_dir, "word2vec.txt"),"w") as writer:# writer.write("%s\n"%(str(output_layer)))hidden_size = output_layer.shape[-1].value # 768#with tf.gfile.GFile(os.path.join(FLAGS.output_dir, "word2vec.txt"),"w") as writer:# writer.write("%s\n"%(str(hidden_size)))output_weights = tf.get_variable("output_weights", [num_labels, hidden_size],initializer=tf.truncated_normal_initializer(stddev=0.02))output_bias = tf.get_variable("output_bias", [num_labels], initializer=tf.zeros_initializer())with tf.variable_scope("loss"):if is_training:# I.e., 0.1 dropoutoutput_layer = tf.nn.dropout(output_layer, keep_prob=0.9)
# with open(os.path.join(FLAGS.output_dir, "output_layer.log"), "a", encoding="utf-8") as f:
# f.write(output_layer)logits = tf.matmul(output_layer, output_weights, transpose_b=True)logits = tf.nn.bias_add(logits, output_bias)probabilities = tf.sigmoid(logits)label_ids = tf.cast(labels, tf.float32)batch_example_loss = tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=label_ids), axis=-1)#對每條記錄的每個標簽都要計算損失再平均最后整個batch記錄再 求平均batch_average_loss = tf.reduce_mean(batch_example_loss)# probabilities = tf.nn.softmax(logits, axis=-1)
# log_probs = tf.nn.log_softmax(logits, axis=-1)# one_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32)# batch_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1)
# batch_loss = tf.reduce_mean(per_example_loss)return (output_layer, loss,batch_example_loss, logits, probabilities)
多分類
def create_model(bert_config, is_training, input_ids, input_mask, segment_ids,labels, num_labels, use_one_hot_embeddings):"""Creates a classification model."""model = modeling.BertModel(config=bert_config,is_training=is_training,input_ids=input_ids,input_mask=input_mask,token_type_ids=segment_ids,use_one_hot_embeddings=use_one_hot_embeddings)# In the demo, we are doing a simple classification task on the entire# segment.## If you want to use the token-level output, use model.get_sequence_output()# instead.output_layer = model.get_pooled_output()# print('output_layer的輸出:',output_layer[:5])hidden_size = output_layer.shape[-1].value # 768output_weights = tf.get_variable("output_weights", [num_labels, hidden_size],initializer=tf.truncated_normal_initializer(stddev=0.02))output_bias = tf.get_variable("output_bias", [num_labels], initializer=tf.zeros_initializer())with tf.variable_scope("loss"):if is_training:# I.e., 0.1 dropoutoutput_layer = tf.nn.dropout(output_layer, keep_prob=0.9)
# with open(os.path.join(FLAGS.output_dir, "output_layer.log"), "a", encoding="utf-8") as f:
# f.write(output_layer)logits = tf.matmul(output_layer, output_weights, transpose_b=True)logits = tf.nn.bias_add(logits, output_bias)probabilities = tf.nn.softmax(logits, axis=-1) #這里不一樣log_probs = tf.nn.log_softmax(logits, axis=-1)#直接轉成 onehot?one_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32)per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1)loss = tf.reduce_mean(per_example_loss)return (output_layer,loss, per_example_loss, logits, probabilities)
利用return 可以把tensorflow的值打印出來
tensorflow多標簽和多分類代碼對比
output_layer = tf.nn.dropout(output_layer, keep_prob=0.9)
# with open(os.path.join(FLAGS.output_dir, "output_layer.log"), "a", encoding="utf-8") as f:
# f.write(output_layer)logits = tf.matmul(output_layer, output_weights, transpose_b=True)logits = tf.nn.bias_add(logits, output_bias)#模型最終輸出值probabilities = tf.nn.softmax(logits, axis=-1) #這里不一樣#經過softmax處理成概率log_probs = tf.nn.log_softmax(logits, axis=-1)#直接轉成 onehot? 相當于sklearn的那個函數?不sklearn是多標簽的轉換 這是只是one hot的轉換one_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32)batch_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1)a=[1,2] b=[2,3]a*b=[1x2,2x3]* 對應位置相乘 batchsize 保存保持不變batch_average_loss = tf.reduce_mean(batch_example_loss)多分類
{"namespace": "erlaojia.avro","type":"record","name":"dataset","fields":[{"name":"id","type":["string","null"]},{"name":"texta","type":"string"},{"name":"textb","type":["string","null"]},{"name":"labels", "type":"string"}]
}schema 相當于規定數據表框架 格式
20210120
Schema對應位置的字段也要更改
如果有字段有空值還的像id字段一樣設默認值為空值
tfrecord問題
20210111
expected dtype float does not equal original dtype doubleInvalidArgumentError (see above for traceback): Restoring from checkpoint failed. This is most likely due to a mismatch between the current graph and the graph from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:答案是路徑沒傳對,初始化類的時候需要傳入路徑
而在類里面卻把路徑寫死了,應該是這里產生的沖突
estimator 的epoch 和steps的關系
epoch 是整個數據集一次
設置了epoch 就不用設置steps了
相反 設置了是 steps了就不用 epoch了
steps=數據總數/batch_size 需要自己去算 很麻煩
20210106
注:為了簡單起見,本示例僅使用標量輸入。要處理非標量特征,最簡單的方法是使用 tf.io.serialize_tensor 將張量轉換為二進制字符串。在 TensorFlow 中,字符串是標量。使用 tf.io.parse_tensor 可將二進制字符串轉換回張量。
https://tensorflow.google.cn/tutorials/load_data/tfrecord?hl=zh_cn
https://blog.csdn.net/briblue/article/details/80789608
tfrecord
https://blog.csdn.net/briblue/article/details/53187780
protocol buffer
flags.mark_flag_as_required(“threshold”)
as_required 提示這個參數是必須輸入的
20210103
nanloss 應是loss 函數寫錯了
tf.argmax
https://blog.csdn.net/u012300744/article/details/81240580
如果要訓練并比較略微不同的模型版本,請為每一個模型版本(code and model_dir)創建一個單獨的文件夾。
Estimators 在訓練過程中會自動將以下內容保存到磁盤:
chenkpoints:訓練過程中的模型快照。
event files:其中包含 TensorBoard 用于創建可視化圖表的信息。
estimator steps 參數用來告訴 train 方法在指定的 training steps 后停止訓練。 一個batch size 為一個steps
average_loss(mean loss per sample)、loss(mean loss per mini-batch)和 estimator 的 global_step (the number of training iterations it underwent)。 epoch*the number of batchsize
20210102
ModuleNotFoundError: No module named ‘tensorflow.compat.v2’
安裝2.2以上版本
20210101
https://blog.csdn.net/u014061630/article/details/83049781
tensorflow estimator 文檔
20201231
InvalidArgumentError (see above for traceback): logits and labels must have the same first dimension, got logits shape [64,2] and labels shape [128]輸入的通道數和實際的數據的通道數不一致
Dense處應該改成2
InvalidArgumentError (see above for traceback): Received a label value of 1 which is outside the valid range of [0, 1). Label values: 1 1 1 0 1 1 0 1 1 0 1 0 0 1 0 1 1最后輸出的類別個數和實際的類別個數不一致
輸入地方定義的image 再灌入模型的時候是需要的
https://blog.csdn.net/jiezhouzi/article/details/81366080
estimator mnist 案例
estimator 的準確率名稱可以自己定義名稱,評估的時候 steps=1
https://blog.csdn.net/u014061630/article/details/83049781
tensorflow文檔
https://blog.csdn.net/yanxiaohui1992/article/details/99960745
#查看tensorflow里面變量具體的值
sess=tf.InteractiveSession()
def change_to_right(wrong_labels):right_labels=[]for x in wrong_labels:for i in range(0,len(wrong_labels[0])):if x[i]==1:right_labels.append(i+1)return right_labelswrong_labels =np.array([[0,0,1,0], [0,0,1,0], [1,0,0,0],[0,1,0,0]])
right_labels =tf.convert_to_tensor(np.array(change_to_right(wrong_labels)))
print(right_labels)tf.global_variables_initializer().run()
aa=sess.run(right_labels)
報錯解決:InvalidArgumentError: Can not squeeze dim[1], expected a dimension of 1, got 101
輸入不能是one_hot形式?
https://blog.csdn.net/zhangpeterx/article/details/89290303
ValueError: Rank mismatch: Rank of labels (received 2) should equal rank of logits minus 1 (received 2).
函數是將softmax和cross_entropy放在一起計算,對于分類問題而言,最后一般都是一個單層全連接神經網絡,比如softmax分類器居多,對這個函數而言,tensorflow神經網絡中是沒有softmax層,而是在這個函數中進行softmax函數的計算。
這里的logits通常是最后的全連接層的輸出結果,labels是具體哪一類的標簽,這個函數是直接使用標簽數據的,而不是采用one-hot編碼形式。
y2 = tf.convert_to_tensor([[0, 0, 1, 0]], dtype=tf.int64)
y_2 = tf.convert_to_tensor([[-2.6, -1.7, 3.2, 0.1]], dtype=tf.float32)
y_3=[3 3 1 2]
標簽數據是y_3 而不是y_2 相當于多分類?
https://www.cnblogs.com/lovychen/p/9439221.html
[tensorflow] tf.nn.sparse_softmax_cross_entropy_with_logits的使用方法及常見報錯
在計算交叉熵之前,通常要用到softmax層來計算結果的概率分布。因為softmax層并不會改變最終的分類結果(排序),所以,tensorflow將softmax層與交叉熵函數進行封裝,形成一個函數方便計算: tf.nn.softmax_cross_entropy_with_logits(logits= , labels=) 。
為了加速計算過程,針對只有一個正確答案(例如MNIST識別)的分類問題,tensorflow提供了 tf.nn.sparse_softmax_cross_entropy_with_logits(logits= , labels=) 。
二者的區別sparse 的y是標簽數據 ,另一個是one_hot 形式
https://blog.csdn.net/u013084616/article/details/79138380
從這里開始都是關于estimator的問題
https://blog.csdn.net/jiezhouzi/article/details/81355903
https://blog.csdn.net/jiezhouzi/article/details/81366080
Estimator Mnist 簡單
20201230
tensorflow.python.framework.errors_impl.InvalidArgumentError: logits and labels must be broadcastable: logits_size=[2,2] labels_size=[0,2][[{{node softmax_cross_entropy_with_logits_sg}}]]數據記錄條數和標簽的個數不相等
20201228
return 處理可以看到真實的值?print 看不到?
我已經看到了很多人對 TensorFlow 的 tf.Graph 和 tf.Session 的規則感到困惑。其實很簡單:
Graph(圖形)定義了計算。但它不計算任何東西,也不包含任何值,它只是定義您在代碼中指定的操作。
Session(會話)允許執行圖形或部分圖形。它為此分配資源(在一臺或多臺機器上)并保存中間結果和變量的實際值。
https://blog.csdn.net/weixin_33725272/article/details/88692031
from tensorflow.python.keras import Sequential
from tensorflow.python.keras.wrappers.scikit_learn import KerasClassifier
from tensorflow.python.keras.utils import np_utils, plot_model
from sklearn.model_selection import cross_val_score, train_test_split, KFold
from sklearn.preprocessing import LabelEncoder
from tensorflow.python.keras.layers import Dense, Dropout, Flatten, Conv1D, MaxPooling1D,BatchNormalization,Dropout
from tensorflow.python.keras.models import model_from_json
# import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import itertools
# import pydot_ng as pydot
import torch
from os import walk
from tqdm import tqdm
import warnings
warnings.simplefilter('ignore')
from tensorflow.python.keras.backend import set_session
import os
from tensorflow.python.keras import models
from sklearn.metrics import classification_reporttensorflow 1.x版本的keras導包
tensorflow 本地加載Mnist
https://www.cnblogs.com/xiexiaokui/p/10409676.html
先下載下來 不要解壓 直接換一下路徑 注意路徑最后不要加斜杠
>>> import tensorflow as tf>>> from tensorflow.examples.tutorials.mnist import input_data>>> MNIST_data =r'D:\tensorflow\mnist'>>> mnist = input_data.read_data_sets(MNIST_data,one_hot=True)
20201226
去掉警告信息下面的
import warnings
warnings.filterwarnings(“ignore”)
import os
os.environ[“TF_CPP_MIN_LOG_LEVEL”] = ‘2’
對于 TF 1.X版本
import tensorflow as tf
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
對于 TF 2.X版本
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)
20201225
https://www.cnblogs.com/xiximayou/p/12690353.html
tensorflow2.0的模型構建方式
20201224
init_op=tf.global_variables_initializer()
with tf.Session() as sess:sess.run(init_op)writer=tf.summary.FileWriter('graphs',sess.graph)a,b=sess.run([a,b])
FileWriter 是什么作用 是為了下面的變量寫一起?
變量的定義將指定變量如何被初始化,但是必須顯式初始化所有的聲明變量。在計算圖的定義中通過聲明初始化操作對象來實現:
對文中所有涉及到的變量同時初始化
#tensorflow 調用GPU
- Tensorflow中指定程序在哪一塊GPU上訓練
Python中代碼:
import os
# 使用第一張與第三張GPU卡
os.environ["CUDA_VISIBLE_DEVICES"] = "0, 2"
命名行代碼:
CUDA_VISIBLE_DEVICES=0,2 python train.py
- 按需增加GPU的內存
import tensorflow as tf#allow growth
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)
# 使用allow_growth option,剛一開始分配少量的GPU容量,然后按需慢慢的增加,由于不會釋放內存,所以會導致碎片
#tensorflow 調用GPU
TF 和 cuda 版本對應問題
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: DLL load failed: 找不到指定的模塊。
20201222
ValueError: Cannot assign to variable dense/kernel:0 due to variable shape (1664, 3) and value shape (1664, 4) are incompatible
keras 的load weight 的路徑有錯
20201221
model.add(Dense(4, activation='softmax'))
dense 是全連接
16 是卷積核 相當于輸出的通道數 3 是卷積核大小 766 是最后輸出的尺寸
768 是輸入的單通道 向量維度 2 是輸入的通道數
20201208
tf.train.Int64List()問題
tf.io.FixedLenFeature函數
https://www.w3cschool.cn/tensorflow_python/tf_io_FixedLenFeature.html
tf.gfile.GFile
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-ys1p2emt.html
tf 只想預測的話 直接把 do_train 和do_predict 改成 false
20201207
tf.cast
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-zfsn2coc.html
20201202
1.公司是讀的數據庫文件然后用 dataprocess 處理成 avro 格式
在linux 里面 通過cat命令可以查看
如果要把avro 轉成 txt 里面文件夾里面有一個 read_avro.py 來讀 然后轉成avro結構
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-l5x72feg.html
tensorflow 手冊
20201130
tf.metrics.accuracy(labels=row_label_ids, predictions=row_predict_ids)
查看準確率
tf.equal(predict_ids, label_ids)
比較兩個數是否相等
20201127
https://www.jianshu.com/p/b636de7c251a
圖和會話的關系
https://blog.csdn.net/kyle1314608/article/details/110236373
會話
https://blog.csdn.net/qq_34113993/article/details/84070293
https://www.cnblogs.com/hypnus-ly/p/8040951.html
計算圖
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-mt3n2no2.html
TensorFlow函數:tf.trainable_variables
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-xp3r2dl5.html
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-8gjs2p9i.html
TensorFlow函數:tf.estimator.ModeKeys
tf.train.Feature
每個數據對象有多種特征
https://blog.csdn.net/kyle1314608/article/details/110235046
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-hckq2htb.html
TensorFlow函數:tf.reduce_mean
https://www.w3cschool.cn/tensorflow_python/tf_nn_sigmoid_cross_entropy_with_logits.html
TensorFlow函數教程:tf.nn.sigmoid_cross_entropy_with_logits
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-5y4d2i2n.html
TensorFlow函數:tf.reduce_sum
減少某個軸再求和
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-5y4d2i2n.html
TensorFlow函數:tf.reduce_sum
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-ht8s2j1k.html
TensorFlow函數:tf.saturate_cast
https://www.w3cschool.cn/tensorflow_python/tf_nn_bias_add.html
TensorFlow函數:tf.nn.bias_add
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-l5x72feg.html
TensorFlow:tf.matmul函數
tf.nn.dropout()是tensorflow里面為了防止或減輕過擬合而使用的函數,它一般用在全連接層
Dropout就是在不同的訓練過程中隨機扔掉一部分神經元。也就是讓某個神經元的激活值以一定的概率p,讓其停止工作,這次訓練過程中不更新權值,也不參加神經網絡的計算。但是它的權重得保留下來(只是暫時不更新而已),因為下次樣本輸入時它可能又得工作了
tf.nn.dropout函數說明
tf.nn.dropout(x,keep_prob,noise_shape=None,seed=None,name=None
)
參數說明:
x:指輸入,輸入tensor
keep_prob: float類型,每個元素被保留下來的概率,設置神經元被選中的概率,在初始化時keep_prob是一個占位符, keep_prob = tf.placeholder(tf.float32) 。tensorflow在run時設置keep_prob具體的值,例如keep_prob: 0.5
noise_shape : 一個1維的int32張量,代表了隨機產生“保留/丟棄”標志的shape。
seed : 整形變量,隨機數種子。
name:指定該操作的名字
dropout必須設置概率keep_prob,并且keep_prob也是一個占位符,跟輸入是一樣的
keep_prob = tf.placeholder(tf.float32)
train的時候才是dropout起作用的時候,test的時候不應該讓dropout起作用
tf.name_scope()、tf.variable_scope()
https://zhuanlan.zhihu.com/p/52055580
作為參考,貌似講的還是有點錯
變量共享主要涉及兩個函數:tf.variable() 和tf.get_variable();即就是必須要在tf.variable_scope的作用域下使用tf.get_variable()函數。這里用tf.get_variable( ) 而不用tf.Variable( ),是因為前者擁有一個變量檢查機制,會檢測已經存在的變量是否設置為共享變量,如果已經存在的變量沒有設置為共享變量(reuse=True),TensorFlow 運行到第二個擁有相同名字的變量的時候,就會報錯。
TF中有兩種作用域類型:命名域 (name scope),通過tf.name_scope 或 tf.op_scope創建;
變量域 (variable scope),通過tf.variable_scope 或 tf.variable_op_scope創建;
這兩種作用域,對于使用tf.Variable()方式創建的變量,具有相同的效果,都會在變量名稱前面,加上域名稱。對于通過tf.get_variable()方式創建的變量,只有variable scope名稱會加到變量名稱前面,而name scope不會作為前綴。
with tf.name_scope("my_name_scope"):v1 = tf.get_variable("var1", [1], dtype=tf.float32) v2 = tf.Variable(1, name="var2", dtype=tf.float32)a = tf.add(v1, v2)print(v1.name)print(v2.name) print(a.name)
var1:0
my_name_scope/var2:0
my_name_scope/Add:0
tf.get_variable的使用方法
參數數量及其作用
該函數共有十一個參數,常用的有:名稱name、變量規格shape、變量類型dtype、變量初始化方式initializer、所屬于的集合collections。
該函數的作用是創建新的tensorflow變量,常見的initializer有:常量初始化器tf.constant_initializer、正太分布初始化器tf.random_normal_initializer、截斷正態分布初始化器tf.truncated_normal_initializer、均勻分布初始化器tf.random_uniform_initializer。
Normal Distribution 稱為正態分布,也稱為高斯分布,Truncated Normal Distribution一般翻譯為截斷正態分布,也有稱為截尾正態分布。
截斷正態分布是截斷分布(Truncated Distribution)的一種,那么截斷分布是什么?截斷分布是指,限制變量x 取值范圍(scope)的一種分布。例如,限制x取值在0到50之間,即{0<x<50}。因此,根據限制條件的不同,截斷分布可以分為:
2.1 限制取值上限,例如,負無窮<x<50
2.2 限制取值下限,例如,0<x<正無窮
2.3 上限下限取值都限制,例如,0<x<50
正態分布則可視為不進行任何截斷的截斷正態分布,也即自變量的取值為負無窮到正無窮;
https://blog.csdn.net/kyle1314608/article/details/110229384
bert 模型的輸出
0、概述
想要獲取獲取bert模型的輸出非常簡單,使用 model.get_sequence_output()和model.get_pooled_output() 兩個方法,但這兩種方法針對NLP的任務需要進行一個選擇
1、output_layer = model.get_sequence_output()
這個獲取每個token的output 輸出[batch_size, seq_length, embedding_size] 如果做seq2seq 或者ner 用這個
2、output_layer = model.get_pooled_output()
這個輸出 是獲取句子的output
3、注意
bert模型對輸入的句子有一個最大長度,對于中文模型,我看到的是512個字。
當我們用model.get_sequence_output()獲取每個單詞的詞向量的時候注意,頭尾是[CLS]和[SEP]的向量。做NER或seq2seq的時候需要注意。
參數drop_remainder:表示在少于batch_size元素的情況下是否應刪除最后一批 ; 默認是不刪除。
tfrecord文件的使用:tf.data.TFRecordDataset
https://blog.csdn.net/yeqiustu/article/details/79793454
tf.contrib.data.map_and_batch
https://blog.csdn.net/heiheiya/article/details/81032997
berf for tf2
pip install bert-for-tf2
編譯過后的tf2
https://github.com/lakshayg/tensorflow-build
https://blog.csdn.net/zaf0516/article/details/103149464
警告信息
WARNING:tensorflow:Entity <bound method TensorFlowOpLayer._defun_call of
成功解決Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
https://blog.csdn.net/qq_41185868/article/details/79127838
pycharm報錯:Process finished with exit code -1073741819 (0xC0000005)(tensorflow學習一)
Tensor conversion requested dtype float64 for Tensor with dtype int32
需求64 得到的卻是32
總結
以上是生活随笔為你收集整理的tensorflow问题的全部內容,希望文章能夠幫你解決所遇到的問題。