小编Jin*_*oss的帖子

当我将其初始化为-1时,为什么我的数组将被填充为零

#include<iostream>
using namespace std;

long long int memo[20] = {-1};       //create memo table  and initialise to -1

long long int fibo(long long int n)
{
    if(memo[n]>-1)           //changing this to if(memo[n]>0) works fine
        return memo[n];      //but as such this gives 0 from all my inputs
    if(n<=2)
        return 1;
    memo[n] =  fibo(n-1) + fibo(n-2);    //recurse
    return memo[n];
}

int main()
{
    long long int n;
    cin>>n;
    cout<<fibo(n)<<endl;       //calls the fibo function 
    for(int i=0;i<20;i++)       //this prints my memo table used...
        cout<<memo[i]<<" ";
} …
Run Code Online (Sandbox Code Playgroud)

c++ arrays initializer

4
推荐指数
1
解决办法
135
查看次数

如何使 ResNet 适应时间序列数据

我正在尝试使用卷积残差网络神经网络架构(ResNet)。到目前为止,我已经使用 Keras 实现了用于时间序列数据分类的简单卷积 (conv1D)。

现在,我正在尝试使用 Keras 构建 ResNet,但在尝试将其适应时间序列数据时遇到了一些困难。Keras 中 ResNet 或 Nasnet 的大多数实现(例如this onethat one)都使用 conv2D 来实现(这对图像有意义)。

有人可以帮助我为时间序列数据实现这个吗?

python machine-learning keras resnet

4
推荐指数
1
解决办法
6684
查看次数

为什么使用太长的初始化程序初始化的字符串会表现得像这样

#include<stdio.h>

void main()
{
    int i;
    char str[4] = "f4dfkjfj";
    char str2[3] = "987";
    char str3[2] = {'j','j','\0'};

    //printf("%c\n",str[1]);
    //printf("%c\n",1[str]);

    puts(str);
    puts(str2);
    puts(str3);           
}
Run Code Online (Sandbox Code Playgroud)

产出观察:

  • 打印str2打印无论从内容str2str.
  • 打印str3打印件str3,str2str.

为什么这个行为在打印非nul时结束了字符串,它与先前定义的字符串连接,直到puts()函数遇到"\ 0"字符(此函数打印直到遇到nul)?

(注意:我故意用太长的初始化字符串初始化它们)

c string pointers

3
推荐指数
1
解决办法
73
查看次数

keras + tensorflow 中的高级自定义激活函数

def newactivation(x):
    if x>0:
        return K.relu(x, alpha=0, max_value=None)
    else :
        return x * K.sigmoid(0.7* x)

get_custom_objects().update({'newactivation': Activation(newactivation)})
Run Code Online (Sandbox Code Playgroud)

我正在尝试在 keras 中为我的模型使用此激活函数,但是我很难找到要替换的内容

if x>0:
Run Code Online (Sandbox Code Playgroud)

我得到的错误:

文件“/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/ops.py”,第 614 行,在bool raise TypeError(“不允许将 atf.Tensor作为 Pythonbool使用。”

类型错误:不允许将 atf.Tensor用作 Python bool。使用if >t is not None:而不是if t:测试是否定义了张量,并且 > 使用 TensorFlow 操作(例如 tf.cond)执行以 > 张量值为条件的子图。

有人可以帮我说清楚吗?

python keras tensorflow activation-function

2
推荐指数
1
解决办法
1742
查看次数

尽管具有相同的算法和数据结构,为什么另一个解决方案的效率是 10 倍?

我在 leetcode 上找到了一段代码,并将其与我的解决方案进行了比较。

问题链接:https : //leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii

Leetcode 解决方案:(16 毫秒和 10.2 MB)

string removeDuplicates1(string s, int k) {
  vector<pair<char, short>> st;
  string res;
  for (auto ch : s) {
    if (st.empty() || st.back().first != ch) st.push_back({ ch, 0 });
    if (++st.back().second == k) st.pop_back();
  }
  for (auto& p : st) res += string(p.second, p.first);
  return res;
}
Run Code Online (Sandbox Code Playgroud)

我的解决方案:(32 毫秒和 132.5 MB)

string removeDuplicates2(string str, int k) {
   if(k == 1)
        return "";
    vector<pair<char, short>> S;
    for(int i=0; i<str.size(); i++) { …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm stack data-structures

2
推荐指数
1
解决办法
82
查看次数

使用字符索引数组时为什么会出现这种不同的行为?

我正在尝试编写一个c ++程序来查找第一个非重复字符,如果所有字符都重复,它将返回-1.

我设计了一个解决方案,使用整数数组跟踪字符数,我使用必须存储其计数的相应字符进行索引.

计划1:提供错误的输出...... http://ide.geeksforgeeks.org/wxOYog

#include<iostream>
#include<cstring>
using namespace std;
int map[256];
string returnFirstRepeatingChar(string str,int n)
{
    int i=0,flag=1;
    string result;
    for(i=0;i<n;i++)
        map[str[i]]++;

    for(i=0;i<n;i++)
    {
        if(map[str[i]]==1)
        {
            flag=0;
            result = str[i];
            break;
        }
    }
    if(flag)
        return "-1";
    else
        return result;
}
int main()
{
    //code
    int t,N,*arr,i,j;
    cin>>t;
    string str;
    while(t--)
    {
        cin>>N;
        memset(&map,0,256);
        cin>>str;
        cout<<returnFirstRepeatingChar(str,N)<<endl;
    } 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

计划2:提供正确的输出...... http://ide.geeksforgeeks.org/jJvJPu

#include<iostream>
#include<cstring>
using namespace std;
int map[256];
string returnFirstRepeatingChar(string str,int n)
{
    int i=0,flag=1;
    string result;
    for(i=0;i<n;i++)
        map[str[i]-97]++; …
Run Code Online (Sandbox Code Playgroud)

c++ arrays string dictionary

1
推荐指数
1
解决办法
57
查看次数