最近我尝试用g ++编译程序(在Ubuntu上).通常我使用Dev-C++(在Windows上),只要我创建一个项目并将所有必要的文件放在那里,它就可以正常工作.
编译程序时发生的错误是:
$filename.cpp: undefined reference to '[Class]::[Class Member Function]'
Run Code Online (Sandbox Code Playgroud)
使用的文件如下:
带有main函数的源代码(.cpp)文件.
带有函数原型的头文件.
.cpp文件,包含每个函数的定义.
任何帮助将不胜感激.
我正在编写一些代码来优化神经网络体系结构,因此有一个python函数create_nn(parms)可以创建和初始化keras模型。但是,我遇到的问题是,经过较少的迭代后,模型的训练时间比平时要长得多(最初一个历时需要10秒,然后大约第14个模型(每个模型训练20个历时)之后,它需要60秒/时代)。我知道这不是因为架构的发展所致,因为如果我重新启动脚本并开始将其结束,则它将恢复正常速度。
我目前正在跑步
from keras import backend as K
Run Code Online (Sandbox Code Playgroud)
然后一个
K.clear_session()
Run Code Online (Sandbox Code Playgroud)
在训练任何给定的新模型之后。
其他一些细节:
对于前12个模型,每个时期的训练时间大致保持恒定,为10秒/时期。然后,在第13个模型中,每个时期的训练时间稳定地上升到60秒。然后每个纪元的训练时间徘徊在大约60秒/纪元。
我正在使用Tensorflow作为后端运行keras
我正在使用Amazon EC2 t2.xlarge实例
有足够的可用RAM(7GB可用,带5GB的数据集)
我删除了很多层和参数,但实际上create_nn看起来像:
def create_nn(features, timesteps, number_of_filters):
inputs = Input(shape = (timesteps, features))
x = GaussianNoise(stddev=0.005)(inputs)
#Layer 1.1
x = Convolution1D(number_of_filters, 3, padding='valid')(x)
x = Activation('relu')(x)
x = Flatten()(x)
x = Dense(10)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Dropout(0.5)(x)
# Output layer
outputs = Dense(1, activation='sigmoid')(x)
model = Model(inputs=inputs, outputs=outputs)
# Compile and Return
model.compile(optimizer = 'adam', loss …Run Code Online (Sandbox Code Playgroud) 我最近制作了一个程序,需要检查用户输入的数字中的位数.结果我做了以下代码:
int x;
cout << "Enter a number: ";
cin >> x;
x /= 10;
while(x > 0)
{
count++;
x = x/10;
}
Run Code Online (Sandbox Code Playgroud)
据我所知(即使我的经验有限),它看起来粗糙而且相当不优雅.
有没有人知道如何改进这个代码(虽然没有使用内置的c ++函数)?
我只是尝试了一些东西并制作了以下代码.它应该用字符串中的每个单独的字母打印其ASCII等效字符.但是,当有空间时,它会停止转换.这是代码:
#include <iostream>
#include <string>
using namespace std;
void convertToASCII(string letter)
{
for (int i = 0; i < letter.length(); i++)
{
char x = letter.at(i);
cout << int(x) << endl;
}
}
int main()
{
string plainText;
cout << "Enter text to convert to ASCII: ";
cin >> plainText;
convertToASCII(plainText);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
关于为什么会这样的想法?
我最近一直在读一本我读过的书.任务是创建一个程序,以二进制,八进制和十六进制等值打印1-256之间的所有数字.我们本来应该只使用我们在本书中学到的方法,这意味着只使用for,while和do..while循环,if和else if语句,将整数转换为ASCII等价物和一些更基本的东西(例如cmath和了iomanip).
所以经过一些工作,这是我的结果.然而,它是杂乱,不优雅和混淆.有没有人有任何建议来提高代码效率(或优雅......:P)和性能?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int decimalValue, binaryValue, octalValue, hexadecimalValue, numberOfDigits;
cout << "Decimal\t\tBinary\t\tOctal\t\tHexadecimal\n\n";
for (int i = 1; i <= 256; i++)
{
binaryValue = 0;
octalValue = 0;
hexadecimalValue = 0;
if (i != 0)
{
int x, j, e, c, r = i, tempBinary, powOfTwo, tempOctal, tempDecimal;
for (j = 0; j <=8; j++) //Starts to convert to binary equivalent
{
x = pow(2.0, j);
if (x …Run Code Online (Sandbox Code Playgroud) 我最近在一本书中读到,使用break例如for循环中的语句被认为是不优雅的(尽管它在switch语句中被广泛接受).
这种"指责"的原因是什么?
我应该break在for循环中使用语句,还是应该使用多个条件?
我正在书中练习,要求我们使用递归方法解决河内塔问题.我已经找到了解决方案,但是在完成浏览互联网后我收集的内容是我的解决方案可能不正确.有谁知道更好/不同的方法来解决问题?有没有人有任何改进的建议.(顺便说一句,输出是正确的.它只能告诉从哪个塔到另一个桩正在移动,而不是具体是哪个钉子)
这是代码:
#include <iostream>
#include <cmath>
using namespace std;
static int counter = 0;
void ToH(int dskToMv, int cLocation, int tmpLocation, int fLocation)
{
if (dskToMv == 0);
else
{
if (dskToMv%2!=0)
{
cout << cLocation << "->" << tmpLocation << endl;
cout << cLocation << "->" << fLocation << endl;
cout << tmpLocation << "->" << fLocation << endl;
ToH(dskToMv-1, cLocation, fLocation, tmpLocation);
}
else if (dskToMv%2==0)
{
counter++;
if (counter%2==0)
cout << fLocation << "->" << cLocation << …Run Code Online (Sandbox Code Playgroud) 我只是想知道一些事情.我有以下代码:
#include <iostream>
using namespace std;
int main()
{
int number, largest, counter = 1;
while (counter <= 10)
{
cout << "Enter a number: ";
cin >> number;
if (counter = 1)
{
largest = number;
}
else if (number > largest)
{
largest = number;
}
counter++;
}
cout << "\n\nThe largest number is: " << largest;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是,它永远不会终止.我确实设法通过稍微修改代码来解决问题,但我想知道为什么会发生这种情况.这是固定代码:
#include <iostream>
using namespace std;
int main()
{
int number, largest, counter = 1;
cout << …Run Code Online (Sandbox Code Playgroud) 我刚刚制作了一个计算pi的程序.然而,即使有1000万次迭代,我的结果也有点不对劲.我得到3.141592 7535897831,而早在早就错了.它应该是3.141592 653589793238 ...
所以我的问题是:在10 ^ -16之间获得至少一个准确答案所需的迭代量是多少
如果有人有兴趣,这是我的代码:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
long double pi = 4.0;
long double tempPi;
for (int i = 1, j = 3; i <= 10000000; i++, j+=2)
{
tempPi = static_cast<double>(4)/j;
if (i%2 != 0)
{
pi -= tempPi;
}
else if (i%2 == 0)
{
pi += tempPi;
}
}
cout << "Pi has the value of: " << setprecision(16) << fixed << pi << endl; …Run Code Online (Sandbox Code Playgroud) 我在一个函数中有这个代码,但是当它运行时它会暂停一段时间,然后它说:
$floating point exception
Run Code Online (Sandbox Code Playgroud)
我假设这是由于for循环中的多个条件,但我不知道为什么它是错误的.有任何想法吗?
int i,j,number=5;
for (i = 2; (i < number || j==1); i++)
{
if (number%i==0)
{
j = 1;
}
}
Run Code Online (Sandbox Code Playgroud) c++ ×9
for-loop ×2
while-loop ×2
algorithm ×1
ascii ×1
break ×1
counting ×1
digits ×1
g++ ×1
keras ×1
optimization ×1
pi ×1
python ×1
recursion ×1
tensorflow ×1