我正在训练使用CNN进行图像分类.由于我的数据集的大小有限,我正在使用转移学习.基本上,我正在使用谷歌在其重新培训示例(https://www.tensorflow.org/tutorials/image_retraining)中证明的预训练网络.
该模型运行良好,并提供非常好的准确性.但我的数据集是高度不平衡的,这意味着准确性不是判断模型性能的最佳指标.
通过研究不同的解决方案,一些人建议改变采样方法或使用的性能指标.我选择和以后一起去.
Tensorflow提供了很好的指标,包括AUC,精度,召回等.
现在,这里是回溯模型的代码:https: //github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/image_retraining/retrain.py
我正在添加以下add_evaluation_step(result_tensor, ground_truth_tensor)功能:
with tf.name_scope('AUC'):
with tf.name_scope('prediction'):
prediction = tf.argmax(result_tensor, 1)
with tf.name_scope('AUC'):
auc_value = tf.metrics.auc(tf.argmax(ground_truth_tensor, 1), prediction, curve='ROC')
tf.summary.scalar('accuracy', evaluation_step)
tf.summary.scalar('AUC', auc_value)
Run Code Online (Sandbox Code Playgroud)
但是我收到了这个错误:
回溯(最近一次调用最后一次):文件"/home/user_2/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/tensorflow/examples/image_retraining/retrain.py",第1135行,tf. app.run(main = main,argv = [sys.argv [0]] + unparsed)文件"/home/user_2/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/tensorflow/python/ platform/app.py",第44行,在运行_sys.exit(main(_sys.argv [:1] + flags_passthrough))文件"/ home/user_2/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain. runfiles/org_tensorflow/tensorflow/examples/image_retraining/retrain.py",第911行,主要是ground_truth_input:train_ground_truth})文件"/home/user_2/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/ tensorflow/python/client/session.py",第767行,运行run_metadata_ptr)文件"/home/user_2/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/tensorflow/python/ client/session.py",第965行,在_run feed_dict_string,options,run_metadata中)文件"/home/user_2/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/tensorflow/python/client/session .py",第1015行,在_do_run target_list,options,run_metadata中)文件"/home/user_2/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/tensorflow/python/client/session.py" ,行1035,在_do_call中提升类型(e)(node_def,op,message)tensorflow.python.framework.errors_impl.FailedPreconditionError:尝试使用未初始化的值AUC/AUC/auc/false_positives
[[Node:AUC/AUC/auc/false_positives/read = IdentityT = DT_FLOAT,_ class = ["loc:@ AUC/AUC/auc/false_positives"],_ device ="/ job:localhost/replica:0/task:0/cpu:0"]]由op u'AUC/AUC/auc/false_positives/read'引起,定义于:File"/home/user_2/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/tensorflow/examples/image_retraining/ retrain.py",第1135行,在tf.app.run中(main = main,argv = [sys.argv [0]] + unparsed)文件"/ home/user_2/tensorflow/bazel-bin/tensorflow/examples/image_retraining /retrain.runfiles/org_tensorflow/tensorflow/python/platform/app.py",第44行,在运行_sys.exit(main(_sys.argv …
我有一个图像数组,其中每个图像都存储为以下维度
(3, 32, 32)
如果我想使用显示图像
plt.imshow(img)
Run Code Online (Sandbox Code Playgroud)
然后我收到以下错误:
类型错误:图像数据的形状 (3, 32, 32) 无效
我明白为什么我会收到此错误,因为根据imshow 文档,它需要一个形状数组
(M,N),或(M,N,3),或(M,N,4)
如何转换图像,使其具有所需的尺寸而不丢失任何数据?
谢谢!
我正在努力创建一个文本分类代码,但我在使用分词器对文档进行编码时遇到了问题。
1)我首先在我的文档上安装了一个标记器,如下所示:
vocabulary_size = 20000
tokenizer = Tokenizer(num_words= vocabulary_size, filters='')
tokenizer.fit_on_texts(df['data'])
Run Code Online (Sandbox Code Playgroud)
2)然后我想检查我的数据是否正确拟合,所以我转换成如下序列:
sequences = tokenizer.texts_to_sequences(df['data'])
data = pad_sequences(sequences, maxlen= num_words)
print(data)
Run Code Online (Sandbox Code Playgroud)
这给了我很好的输出。即将单词编码为数字
[[ 9628 1743 29 ... 161 52 250]
[14948 1 70 ... 31 108 78]
[ 2207 1071 155 ... 37607 37608 215]
...
[ 145 74 947 ... 1 76 21]
[ 95 11045 1244 ... 693 693 144]
[ 11 133 61 ... 87 57 24]]
Run Code Online (Sandbox Code Playgroud)
现在,我想使用相同的方法将文本转换为序列。像这样:
sequences = tokenizer.texts_to_sequences("physics is nice ")
text = pad_sequences(sequences, …Run Code Online (Sandbox Code Playgroud) Tensorflow发布了一个用于迁移学习的教程,名为Image retraining,可以在以下位置找到:https://www.tensorflow.org/tutorials/image_retraining
他们正在做的是在Inception v3上使用预先训练的模型,然后他们仅更改最后一层(softmax回归层)并将其训练到新数据集上。这是非常容易理解的,实际上是迁移学习中的常见做法。
我已经在我的数据集(这是一个很小的数据集)上尝试了他们的方法,并且我已经应用了所有建议以从数据扩充中获得更好的结果来更改步骤数,但是我没有以任何方式修改他们的代码。我得到的准确度相对较差〜70%。
我正在考虑在给定模型的顶部训练小型神经网络的可能性,即将最后一层从简单回归更改为更复杂的网络。
这是他们的代码部分,用于修改softmax层:
def add_final_training_ops(class_count, final_tensor_name, bottleneck_tensor):
"""Adds a new softmax and fully-connected layer for training.
We need to retrain the top layer to identify our new classes, so this function
adds the right operations to the graph, along with some variables to hold the
weights, and then sets up all the gradients for the backward pass.
The set up for the softmax and fully-connected layers is based on:
https://tensorflow.org/versions/master/tutorials/mnist/beginners/index.html
Args:
class_count: Integer of …Run Code Online (Sandbox Code Playgroud) 我想在Jupyter中更改内核的路径,以将其设置为我在计算机中使用的内核。
这是在jupyter中安装的原始内核:
jupyter kernelspec list
Run Code Online (Sandbox Code Playgroud)
可用的内核:
python3 /home/n/.local/share/jupyter/kernels/python3
python2 / usr / local / share / jupyter / kernels / python2
然后我像这样检查我机器中python3的路径:
which python3
Run Code Online (Sandbox Code Playgroud)
/ usr / bin / python3
$ ipython kernelspec列表
我这样删除了python3的内核:
jupyter kernelspec uninstall python3
Run Code Online (Sandbox Code Playgroud)
它成功执行,然后我尝试使用之前找到的路径安装python3,如下所示:
jupyter kernelspec安装/ usr / bin / python3
这给了我这个错误:
sys.exit(KernelSpecApp.launch_instance())中
文件“ / usr / local / bin / jupyter-kernelspec”,第11行,文件“ /usr/local/lib/python2.7/dist-packages/traitlets/config/application” .py“,第658行,在launch_instance app.start()中,
文件“ /usr/local/lib/python2.7/dist-packages/jupyter_client/kernelspecapp.py”,在第273行,在start中返回self.subapp.start( )
在开始replace = self.replace文件“ /usr/local/lib/python2.7/dist-packages/jupyter_client/kernelspecapp.py”第143行中,
文件“ /usr/local/lib/python2.7/dist” -packages / jupyter_client / kernelspec.py“,行299,在install_kernel_spec shutil.copytree(source_dir,destination)中,
文件” /usr/lib/python2.7/shutil.py“,行171,在copytree中名称= os.listdir(src)OSError:[Errno 20]不是目录:'/ …
我正在使用precision_recall_fscore_supportfromsklearn来计算微精度和微召回。
问题是该函数为它们返回完全相同的值。这是一个多类分类问题,我不确定出了什么问题。
这是代码:
t = precision_recall_fscore_support(y_test, classifier.predict(x_test), average='micro')
print(t)
Run Code Online (Sandbox Code Playgroud)
这是输出:
微精度:(0.3359375、0.3359375、0.3359375、无)
我正在尝试训练决策树模型,保存它,然后在以后需要时重新加载它。但是,我不断收到以下错误:
该DecisionTreeClassifier实例尚未安装。使用此方法之前,请使用适当的参数调用“ fit”。
这是我的代码:
X_train, X_test, y_train, y_test = train_test_split(data, label, test_size=0.20, random_state=4)
names = ["Decision Tree", "Random Forest", "Neural Net"]
classifiers = [
DecisionTreeClassifier(),
RandomForestClassifier(),
MLPClassifier()
]
score = 0
for name, clf in zip(names, classifiers):
if name == "Decision Tree":
clf = DecisionTreeClassifier(random_state=0)
grid_search = GridSearchCV(clf, param_grid=param_grid_DT)
grid_search.fit(X_train, y_train_TF)
if grid_search.best_score_ > score:
score = grid_search.best_score_
best_clf = clf
elif name == "Random Forest":
clf = RandomForestClassifier(random_state=0)
grid_search = GridSearchCV(clf, param_grid_RF)
grid_search.fit(X_train, y_train_TF)
if grid_search.best_score_ > score:
score …Run Code Online (Sandbox Code Playgroud) python machine-learning scikit-learn cross-validation grid-search
我正在尝试编写一个函数来计算给定数字的阶乘,我定义如下:
unsigned long long int fact(unsigned long long int n){
return (n == 0 || n == 1) ? 1 : fact(n - 1) * n;
}
Run Code Online (Sandbox Code Playgroud)
我的主要职责是:
int main(int argc, const char * argv[]) {
unsigned long long int z;
cout << "Please eneter the number: ";
cin >> z;
z = fact(z);
cout << z << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
代码工作正常,直到20,但之后返回垃圾值.
我知道我可以使用数组解决问题,但我不明白为什么程序运行在这样的行为?
我正在探索C++代码cstring内部的工作.常用的功能之一是.strrev()
但我发现在Mac和Linux上这个函数被认为是未定义的,即使我包含头文件<cstring>或<string.h>.
但是,该功能在Windows上运行正常.
(我知道我可以自己定义函数,因为此处提供的解决方案在Linux上不可用)
这可能是一个非常天真的问题,但不是不同平台上的头文件的定义应该是相同的吗?或者每个平台都有不同的头文件定义?
另一个奇怪的事情是我strrev()在<cstring>头文件的描述中没有找到(cstring头文件描述),但是如果我在Windows上没有包含这个头文件我得到一个错误,当我包含它时错误就解决了.
我试图通过执行以下操作从数据框中的列中删除标点符号:
def remove_punctuation(text):
return text.translate(table)
df['data'] = df['data'].map(lambda x: remove_punctuation(x))
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
“float”对象没有属性“translate”
我检查了 col 的 dtype,如下所示:
from pandas.api.types import is_string_dtype
is_string_dtype(df['data'])
Run Code Online (Sandbox Code Playgroud)
并得到以下输出:
真的
我不确定这里出了什么问题?
我还尝试了以下操作:translator = str.maketrans('', '', string.punctuation)
def remove_punctuation(text):
return text.translate(translator)
df['data'] = df['data'].map(lambda x: remove_punctuation(x))
Run Code Online (Sandbox Code Playgroud)
但我仍然遇到同样的错误
python ×6
c++ ×2
scikit-learn ×2
tensorflow ×2
c++11 ×1
c-strings ×1
grid-search ×1
imshow ×1
jupyter ×1
keras ×1
linux ×1
matplotlib ×1
nlp ×1
pandas ×1
python-3.x ×1
string ×1
tokenize ×1